From e088e283afc940bb7a23a839180f604bc63c2699 Mon Sep 17 00:00:00 2001 From: James Hall Date: Sun, 6 Oct 2024 12:47:10 +0100 Subject: [PATCH] fmt --- build.gradle | 24 -- common/build.gradle | 28 ++ .../mc/duzo/fakeplayer/FakePlayerMod.java | 1 + .../java/mc/duzo/fakeplayer/Register.java | 57 ++- .../client/FakePlayerModClient.java | 7 +- .../client/model/HumanoidEntityModel.java | 9 +- .../renderer/FakePlayerEntityRenderer.java | 35 +- .../renderer/HumanoidEntityRenderer.java | 39 +- .../fakeplayer/entity/FakePlayerEntity.java | 112 ++--- .../fakeplayer/entity/HumanoidEntity.java | 42 +- .../mc/duzo/fakeplayer/network/FPNetwork.java | 13 +- .../network/s2c/SendSkinS2CPacket.java | 44 +- .../mc/duzo/fakeplayer/util/SkinGrabber.java | 382 +++++++++--------- versioning.gradle | 6 +- 14 files changed, 406 insertions(+), 393 deletions(-) diff --git a/build.gradle b/build.gradle index b1c31f7..bcfe805 100644 --- a/build.gradle +++ b/build.gradle @@ -56,27 +56,3 @@ allprojects { withSourcesJar() } } - -spotless { - format 'misc', { - // define the files to apply `misc` to - target '*.gradle', '.gitattributes', '.gitignore' - - // define the steps to apply to those files - trimTrailingWhitespace() - indentWithSpaces() - endWithNewline() - } - - java { - //eclipse() - - removeUnusedImports() - importOrder('java', 'javax', '', 'net.minecraft', group) - - indentWithSpaces() - trimTrailingWhitespace() - - formatAnnotations() - } -} diff --git a/common/build.gradle b/common/build.gradle index 74d3195..3afd39f 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -1,3 +1,7 @@ +plugins { + id "com.diffplug.spotless" version "6.20.0" +} + architectury { common(rootProject.enabled_platforms.split(",")) } @@ -28,3 +32,27 @@ publishing { // Add repositories to publish to here. } } + +spotless { + format 'misc', { + // define the files to apply `misc` to + target '*.gradle', '.gitattributes', '.gitignore' + + // define the steps to apply to those files + trimTrailingWhitespace() + indentWithSpaces() + endWithNewline() + } + + java { + //eclipse() + + removeUnusedImports() + importOrder('java', 'javax', '', 'net.minecraft', group) + + indentWithSpaces() + trimTrailingWhitespace() + + formatAnnotations() + } +} diff --git a/common/src/main/java/mc/duzo/fakeplayer/FakePlayerMod.java b/common/src/main/java/mc/duzo/fakeplayer/FakePlayerMod.java index 1b1e7ac..2595f50 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/FakePlayerMod.java +++ b/common/src/main/java/mc/duzo/fakeplayer/FakePlayerMod.java @@ -2,6 +2,7 @@ import dev.architectury.platform.Platform; import dev.architectury.utils.Env; + import mc.duzo.fakeplayer.client.FakePlayerModClient; import mc.duzo.fakeplayer.network.FPNetwork; diff --git a/common/src/main/java/mc/duzo/fakeplayer/Register.java b/common/src/main/java/mc/duzo/fakeplayer/Register.java index 0ec015a..a7526b0 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/Register.java +++ b/common/src/main/java/mc/duzo/fakeplayer/Register.java @@ -1,49 +1,48 @@ package mc.duzo.fakeplayer; +import static mc.duzo.fakeplayer.FakePlayerMod.MOD_ID; + +import java.util.function.Supplier; + import com.google.common.base.Suppliers; import dev.architectury.registry.CreativeTabRegistry; import dev.architectury.registry.level.entity.EntityAttributeRegistry; import dev.architectury.registry.registries.DeferredRegister; import dev.architectury.registry.registries.RegistrarManager; import dev.architectury.registry.registries.RegistrySupplier; -import mc.duzo.fakeplayer.entity.FakePlayerEntity; + import net.minecraft.entity.EntityType; import net.minecraft.entity.SpawnGroup; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; -import net.minecraft.registry.Registries; import net.minecraft.registry.RegistryKeys; import net.minecraft.text.Text; -import net.minecraft.world.GameRules; - -import java.util.function.Supplier; - -import static mc.duzo.fakeplayer.FakePlayerMod.MOD_ID; +import mc.duzo.fakeplayer.entity.FakePlayerEntity; public class Register { - public static final Supplier REGISTRIES = Suppliers.memoize(() -> RegistrarManager.get(MOD_ID)); - - // Registering a new creative tab - public static final DeferredRegister TABS = DeferredRegister.create(MOD_ID, RegistryKeys.ITEM_GROUP); - public static final RegistrySupplier FP_TAB = TABS.register("tab", () -> - CreativeTabRegistry.create(Text.translatable("itemGroup." + MOD_ID + ".tab"), - () -> new ItemStack(Items.IRON_INGOT))); - - public static final DeferredRegister ITEMS = DeferredRegister.create(MOD_ID, RegistryKeys.ITEM); - public static final DeferredRegister> ENTITIES = DeferredRegister.create(MOD_ID, RegistryKeys.ENTITY_TYPE); - public static final RegistrySupplier> FAKE_PLAYER = ENTITIES.register("fake_player", () -> EntityType.Builder - .create(FakePlayerEntity::new, SpawnGroup.MISC) - .setDimensions(0.6f, 1.8f) - .build("fake_player")); - - public static void init() { - ENTITIES.register(); - TABS.register(); - ITEMS.register(); - - EntityAttributeRegistry.register(FAKE_PLAYER, FakePlayerEntity::getHumanoidAttributes); - } + public static final Supplier REGISTRIES = Suppliers.memoize(() -> RegistrarManager.get(MOD_ID)); + + // Registering a new creative tab + public static final DeferredRegister TABS = DeferredRegister.create(MOD_ID, RegistryKeys.ITEM_GROUP); + public static final RegistrySupplier FP_TAB = TABS.register("tab", () -> + CreativeTabRegistry.create(Text.translatable("itemGroup." + MOD_ID + ".tab"), + () -> new ItemStack(Items.IRON_INGOT))); + + public static final DeferredRegister ITEMS = DeferredRegister.create(MOD_ID, RegistryKeys.ITEM); + public static final DeferredRegister> ENTITIES = DeferredRegister.create(MOD_ID, RegistryKeys.ENTITY_TYPE); + public static final RegistrySupplier> FAKE_PLAYER = ENTITIES.register("fake_player", () -> EntityType.Builder + .create(FakePlayerEntity::new, SpawnGroup.MISC) + .setDimensions(0.6f, 1.8f) + .build("fake_player")); + + public static void init() { + ENTITIES.register(); + TABS.register(); + ITEMS.register(); + + EntityAttributeRegistry.register(FAKE_PLAYER, FakePlayerEntity::getHumanoidAttributes); + } } diff --git a/common/src/main/java/mc/duzo/fakeplayer/client/FakePlayerModClient.java b/common/src/main/java/mc/duzo/fakeplayer/client/FakePlayerModClient.java index 5a008c1..c61d8a3 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/client/FakePlayerModClient.java +++ b/common/src/main/java/mc/duzo/fakeplayer/client/FakePlayerModClient.java @@ -1,11 +1,12 @@ package mc.duzo.fakeplayer.client; import dev.architectury.registry.client.level.entity.EntityRendererRegistry; + import mc.duzo.fakeplayer.Register; import mc.duzo.fakeplayer.client.renderer.FakePlayerEntityRenderer; public class FakePlayerModClient { - public static void init() { - EntityRendererRegistry.register(Register.FAKE_PLAYER, FakePlayerEntityRenderer::new); - } + public static void init() { + EntityRendererRegistry.register(Register.FAKE_PLAYER, FakePlayerEntityRenderer::new); + } } diff --git a/common/src/main/java/mc/duzo/fakeplayer/client/model/HumanoidEntityModel.java b/common/src/main/java/mc/duzo/fakeplayer/client/model/HumanoidEntityModel.java index a3bb86e..e69d07f 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/client/model/HumanoidEntityModel.java +++ b/common/src/main/java/mc/duzo/fakeplayer/client/model/HumanoidEntityModel.java @@ -1,11 +1,12 @@ package mc.duzo.fakeplayer.client.model; -import mc.duzo.fakeplayer.entity.HumanoidEntity; import net.minecraft.client.model.ModelPart; import net.minecraft.client.render.entity.model.PlayerEntityModel; +import mc.duzo.fakeplayer.entity.HumanoidEntity; + public class HumanoidEntityModel extends PlayerEntityModel { - public HumanoidEntityModel(ModelPart root, boolean thinArms) { - super(root, thinArms); - } + public HumanoidEntityModel(ModelPart root, boolean thinArms) { + super(root, thinArms); + } } \ No newline at end of file diff --git a/common/src/main/java/mc/duzo/fakeplayer/client/renderer/FakePlayerEntityRenderer.java b/common/src/main/java/mc/duzo/fakeplayer/client/renderer/FakePlayerEntityRenderer.java index 70dc0df..baf38d1 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/client/renderer/FakePlayerEntityRenderer.java +++ b/common/src/main/java/mc/duzo/fakeplayer/client/renderer/FakePlayerEntityRenderer.java @@ -1,32 +1,33 @@ package mc.duzo.fakeplayer.client.renderer; +import net.minecraft.client.render.entity.EntityRendererFactory; +import net.minecraft.util.Identifier; + import mc.duzo.fakeplayer.client.model.HumanoidEntityModel; import mc.duzo.fakeplayer.entity.FakePlayerEntity; import mc.duzo.fakeplayer.entity.HumanoidEntity; import mc.duzo.fakeplayer.util.SkinGrabber; -import net.minecraft.client.render.entity.EntityRendererFactory; -import net.minecraft.util.Identifier; public class FakePlayerEntityRenderer extends HumanoidEntityRenderer { - public FakePlayerEntityRenderer(EntityRendererFactory.Context context) { - super(context, false); - } + public FakePlayerEntityRenderer(EntityRendererFactory.Context context) { + super(context, false); + } - @Override - public Identifier getTexture(HumanoidEntity entity) { - if (entity.getCustomName() == null || entity.getCustomName().getString().isBlank()) { - // If the name is the default blank one, send back the error - return SkinGrabber.ERROR_TEXTURE; - } + @Override + public Identifier getTexture(HumanoidEntity entity) { + if (entity.getCustomName() == null || entity.getCustomName().getString().isBlank()) { + // If the name is the default blank one, send back the error + return SkinGrabber.ERROR_TEXTURE; + } - Identifier texture = SkinGrabber.getEntitySkinFromList(entity); + Identifier texture = SkinGrabber.getEntitySkinFromList(entity); - if (texture != null) { - return texture; - } + if (texture != null) { + return texture; + } - return SkinGrabber.ERROR_TEXTURE; - } + return SkinGrabber.ERROR_TEXTURE; + } } \ No newline at end of file diff --git a/common/src/main/java/mc/duzo/fakeplayer/client/renderer/HumanoidEntityRenderer.java b/common/src/main/java/mc/duzo/fakeplayer/client/renderer/HumanoidEntityRenderer.java index 509b0e2..7a59753 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/client/renderer/HumanoidEntityRenderer.java +++ b/common/src/main/java/mc/duzo/fakeplayer/client/renderer/HumanoidEntityRenderer.java @@ -1,8 +1,5 @@ package mc.duzo.fakeplayer.client.renderer; -import mc.duzo.fakeplayer.FakePlayerMod; -import mc.duzo.fakeplayer.client.model.HumanoidEntityModel; -import mc.duzo.fakeplayer.entity.HumanoidEntity; import net.minecraft.client.render.VertexConsumerProvider; import net.minecraft.client.render.entity.EntityRendererFactory; import net.minecraft.client.render.entity.LivingEntityRenderer; @@ -14,25 +11,29 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; +import mc.duzo.fakeplayer.FakePlayerMod; +import mc.duzo.fakeplayer.client.model.HumanoidEntityModel; +import mc.duzo.fakeplayer.entity.HumanoidEntity; + public abstract class HumanoidEntityRenderer> extends LivingEntityRenderer { - protected HumanoidEntityRenderer(EntityRendererFactory.Context context, boolean isSlim) { - super(context, new HumanoidEntityModel(context.getPart(EntityModelLayers.PLAYER), isSlim), 0.5f); - this.addFeature(new ArmorFeatureRenderer(this, new ArmorEntityModel(context.getPart(EntityModelLayers.PLAYER_INNER_ARMOR)), new ArmorEntityModel(context.getPart(EntityModelLayers.PLAYER_OUTER_ARMOR)), context.getModelManager())); - this.addFeature(new HeldItemFeatureRenderer<>(this, context.getHeldItemRenderer())); - } + protected HumanoidEntityRenderer(EntityRendererFactory.Context context, boolean isSlim) { + super(context, new HumanoidEntityModel(context.getPart(EntityModelLayers.PLAYER), isSlim), 0.5f); + this.addFeature(new ArmorFeatureRenderer(this, new ArmorEntityModel(context.getPart(EntityModelLayers.PLAYER_INNER_ARMOR)), new ArmorEntityModel(context.getPart(EntityModelLayers.PLAYER_OUTER_ARMOR)), context.getModelManager())); + this.addFeature(new HeldItemFeatureRenderer<>(this, context.getHeldItemRenderer())); + } - @Override - public void render(HumanoidEntity livingEntity, float f, float g, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i) { - matrixStack.push(); + @Override + public void render(HumanoidEntity livingEntity, float f, float g, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i) { + matrixStack.push(); - matrixStack.scale(0.9375F, 0.9375F, 0.9375F); + matrixStack.scale(0.9375F, 0.9375F, 0.9375F); - super.render(livingEntity, f, g, matrixStack, vertexConsumerProvider, i); - matrixStack.pop(); - } + super.render(livingEntity, f, g, matrixStack, vertexConsumerProvider, i); + matrixStack.pop(); + } - @Override - public Identifier getTexture(HumanoidEntity entity) { - return new Identifier(FakePlayerMod .MOD_ID,"textures/entity/humanoid/humanoid.png"); - } + @Override + public Identifier getTexture(HumanoidEntity entity) { + return new Identifier(FakePlayerMod .MOD_ID,"textures/entity/humanoid/humanoid.png"); + } } \ No newline at end of file diff --git a/common/src/main/java/mc/duzo/fakeplayer/entity/FakePlayerEntity.java b/common/src/main/java/mc/duzo/fakeplayer/entity/FakePlayerEntity.java index 873a337..f4a7784 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/entity/FakePlayerEntity.java +++ b/common/src/main/java/mc/duzo/fakeplayer/entity/FakePlayerEntity.java @@ -1,9 +1,8 @@ package mc.duzo.fakeplayer.entity; -import mc.duzo.fakeplayer.network.FPNetwork; -import mc.duzo.fakeplayer.network.s2c.SendSkinS2CPacket; -import mc.duzo.fakeplayer.util.SkinGrabber; +import org.jetbrains.annotations.Nullable; + import net.minecraft.entity.EntityType; import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.TrackedData; @@ -11,58 +10,61 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.world.World; -import org.jetbrains.annotations.Nullable; + +import mc.duzo.fakeplayer.network.FPNetwork; +import mc.duzo.fakeplayer.network.s2c.SendSkinS2CPacket; +import mc.duzo.fakeplayer.util.SkinGrabber; public class FakePlayerEntity extends HumanoidEntity { - private static final TrackedData SKIN_URL = DataTracker.registerData(FakePlayerEntity.class, TrackedDataHandlerRegistry.STRING); - private static final TrackedData SLIM = DataTracker.registerData(FakePlayerEntity.class, TrackedDataHandlerRegistry.BOOLEAN); - - public FakePlayerEntity(EntityType entityType, World world) { - super(entityType, world); - } - - @Override - protected void initDataTracker() { - super.initDataTracker(); - - this.dataTracker.startTracking(SKIN_URL,""); - this.dataTracker.startTracking(SLIM, false); - } - - @Override - public boolean canPickUpLoot() { - return true; - } - - private void setSlim(boolean slim) { - this.dataTracker.set(SLIM, slim); - } - public boolean isSlim() { - return this.dataTracker.get(SLIM); - } - - private void setURL(String url, boolean sync) { - this.dataTracker.set(SKIN_URL, url); - - if (sync) this.syncURL(); - } - public String getURL() { - return this.dataTracker.get(SKIN_URL); - } - private void syncURL() { - if (this.getWorld().isClient()) return; - - FPNetwork.CHANNEL.sendToPlayers((Iterable) this.getWorld().getPlayers(), new SendSkinS2CPacket(this)); - } - - @Override - public void setCustomName(@Nullable Text name) { - super.setCustomName(name); - - if (this.getWorld().isClient()) return; - if (name == null) return; - if (name.getString().isBlank()) return; - - this.setURL(SkinGrabber.URL + name.getString(), true); - } + private static final TrackedData SKIN_URL = DataTracker.registerData(FakePlayerEntity.class, TrackedDataHandlerRegistry.STRING); + private static final TrackedData SLIM = DataTracker.registerData(FakePlayerEntity.class, TrackedDataHandlerRegistry.BOOLEAN); + + public FakePlayerEntity(EntityType entityType, World world) { + super(entityType, world); + } + + @Override + protected void initDataTracker() { + super.initDataTracker(); + + this.dataTracker.startTracking(SKIN_URL,""); + this.dataTracker.startTracking(SLIM, false); + } + + @Override + public boolean canPickUpLoot() { + return true; + } + + private void setSlim(boolean slim) { + this.dataTracker.set(SLIM, slim); + } + public boolean isSlim() { + return this.dataTracker.get(SLIM); + } + + private void setURL(String url, boolean sync) { + this.dataTracker.set(SKIN_URL, url); + + if (sync) this.syncURL(); + } + public String getURL() { + return this.dataTracker.get(SKIN_URL); + } + private void syncURL() { + if (this.getWorld().isClient()) return; + + FPNetwork.CHANNEL.sendToPlayers((Iterable) this.getWorld().getPlayers(), new SendSkinS2CPacket(this)); + } + + @Override + public void setCustomName(@Nullable Text name) { + super.setCustomName(name); + + if (this.getWorld().isClient()) return; + if (name == null) return; + if (name.getString().isBlank()) return; + + this.setURL(SkinGrabber.URL + name.getString(), true); + } } diff --git a/common/src/main/java/mc/duzo/fakeplayer/entity/HumanoidEntity.java b/common/src/main/java/mc/duzo/fakeplayer/entity/HumanoidEntity.java index 0d31675..3cdd9e9 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/entity/HumanoidEntity.java +++ b/common/src/main/java/mc/duzo/fakeplayer/entity/HumanoidEntity.java @@ -10,28 +10,28 @@ import net.minecraft.world.World; public abstract class HumanoidEntity extends PathAwareEntity { - protected HumanoidEntity(EntityType entityType, World world) { - super(entityType, world); - } + protected HumanoidEntity(EntityType entityType, World world) { + super(entityType, world); + } - @Override - protected void initGoals() { - super.initGoals(); + @Override + protected void initGoals() { + super.initGoals(); - this.goalSelector.add(5, new WanderAroundPointOfInterestGoal(this, 0.5D,false)); - this.goalSelector.add(5, new WanderAroundGoal(this,0.5D,120,false)); - this.goalSelector.add(5, new LookAroundGoal(this)); - this.goalSelector.add(5, new LookAtEntityGoal(this, PlayerEntity.class, 8f)); - this.targetSelector.add(2, new RevengeGoal(this)); - ((MobNavigation)this.getNavigation()).setCanPathThroughDoors(true); - this.getNavigation().setCanSwim(true); - this.setCanPickUpLoot(true); - this.goalSelector.add(2, new WanderNearTargetGoal(this, 0.5D, 32.0f)); - this.goalSelector.add(2, new MeleeAttackGoal(this, 0.5D,true)); - this.goalSelector.add(2, new SwimGoal(this)); - } + this.goalSelector.add(5, new WanderAroundPointOfInterestGoal(this, 0.5D,false)); + this.goalSelector.add(5, new WanderAroundGoal(this,0.5D,120,false)); + this.goalSelector.add(5, new LookAroundGoal(this)); + this.goalSelector.add(5, new LookAtEntityGoal(this, PlayerEntity.class, 8f)); + this.targetSelector.add(2, new RevengeGoal(this)); + ((MobNavigation)this.getNavigation()).setCanPathThroughDoors(true); + this.getNavigation().setCanSwim(true); + this.setCanPickUpLoot(true); + this.goalSelector.add(2, new WanderNearTargetGoal(this, 0.5D, 32.0f)); + this.goalSelector.add(2, new MeleeAttackGoal(this, 0.5D,true)); + this.goalSelector.add(2, new SwimGoal(this)); + } - public static DefaultAttributeContainer.Builder getHumanoidAttributes() { - return DefaultAttributeContainer.builder().add(EntityAttributes.GENERIC_ATTACK_KNOCKBACK).add(EntityAttributes.GENERIC_ATTACK_DAMAGE).add(EntityAttributes.GENERIC_FOLLOW_RANGE).add(EntityAttributes.GENERIC_MAX_HEALTH).add(EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE).add(EntityAttributes.GENERIC_MOVEMENT_SPEED).add(EntityAttributes.GENERIC_ARMOR).add(EntityAttributes.GENERIC_ARMOR_TOUGHNESS); - } + public static DefaultAttributeContainer.Builder getHumanoidAttributes() { + return DefaultAttributeContainer.builder().add(EntityAttributes.GENERIC_ATTACK_KNOCKBACK).add(EntityAttributes.GENERIC_ATTACK_DAMAGE).add(EntityAttributes.GENERIC_FOLLOW_RANGE).add(EntityAttributes.GENERIC_MAX_HEALTH).add(EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE).add(EntityAttributes.GENERIC_MOVEMENT_SPEED).add(EntityAttributes.GENERIC_ARMOR).add(EntityAttributes.GENERIC_ARMOR_TOUGHNESS); + } } diff --git a/common/src/main/java/mc/duzo/fakeplayer/network/FPNetwork.java b/common/src/main/java/mc/duzo/fakeplayer/network/FPNetwork.java index dbefc1d..f53e181 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/network/FPNetwork.java +++ b/common/src/main/java/mc/duzo/fakeplayer/network/FPNetwork.java @@ -1,15 +1,16 @@ package mc.duzo.fakeplayer.network; import dev.architectury.networking.NetworkChannel; + +import net.minecraft.util.Identifier; + import mc.duzo.fakeplayer.FakePlayerMod; -import mc.duzo.fakeplayer.entity.FakePlayerEntity; import mc.duzo.fakeplayer.network.s2c.SendSkinS2CPacket; -import net.minecraft.util.Identifier; public class FPNetwork { - public static final NetworkChannel CHANNEL = NetworkChannel.create(new Identifier(FakePlayerMod.MOD_ID, "main")); + public static final NetworkChannel CHANNEL = NetworkChannel.create(new Identifier(FakePlayerMod.MOD_ID, "main")); - public static void init() { - CHANNEL.register(SendSkinS2CPacket.class, SendSkinS2CPacket::write, SendSkinS2CPacket::new, SendSkinS2CPacket::handle); - } + public static void init() { + CHANNEL.register(SendSkinS2CPacket.class, SendSkinS2CPacket::write, SendSkinS2CPacket::new, SendSkinS2CPacket::handle); + } } diff --git a/common/src/main/java/mc/duzo/fakeplayer/network/s2c/SendSkinS2CPacket.java b/common/src/main/java/mc/duzo/fakeplayer/network/s2c/SendSkinS2CPacket.java index eaefb4b..4e8f2b6 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/network/s2c/SendSkinS2CPacket.java +++ b/common/src/main/java/mc/duzo/fakeplayer/network/s2c/SendSkinS2CPacket.java @@ -1,31 +1,31 @@ package mc.duzo.fakeplayer.network.s2c; +import java.io.File; +import java.util.function.Supplier; + import dev.architectury.networking.NetworkManager; -import mc.duzo.fakeplayer.entity.FakePlayerEntity; -import mc.duzo.fakeplayer.util.SkinGrabber; -import net.minecraft.client.network.ClientPlayerEntity; + import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.Packet; -import java.io.File; -import java.util.function.Supplier; +import mc.duzo.fakeplayer.entity.FakePlayerEntity; +import mc.duzo.fakeplayer.util.SkinGrabber; public record SendSkinS2CPacket(String uuid, String path, String name, String url) { - public SendSkinS2CPacket(FakePlayerEntity entity) { - this(entity.getUuidAsString(), SkinGrabber.DEFAULT_DIR, entity.getUuidAsString(), entity.getURL()); - } - public SendSkinS2CPacket(PacketByteBuf buf) { - this(buf.readString(), buf.readString(), buf.readString(), buf.readString()); - } + public SendSkinS2CPacket(FakePlayerEntity entity) { + this(entity.getUuidAsString(), SkinGrabber.DEFAULT_DIR, entity.getUuidAsString(), entity.getURL()); + } + public SendSkinS2CPacket(PacketByteBuf buf) { + this(buf.readString(), buf.readString(), buf.readString(), buf.readString()); + } - public void write(PacketByteBuf buf) { - buf.writeString(uuid); - buf.writeString(path); - buf.writeString(name); - buf.writeString(url); - } - public void handle(Supplier context) { - SkinGrabber.downloadImageFromURL(name.toLowerCase().replace(" ", ""), new File(path), url); - SkinGrabber.addCustomNameToList(name); - } + public void write(PacketByteBuf buf) { + buf.writeString(uuid); + buf.writeString(path); + buf.writeString(name); + buf.writeString(url); + } + public void handle(Supplier context) { + SkinGrabber.downloadImageFromURL(name.toLowerCase().replace(" ", ""), new File(path), url); + SkinGrabber.addCustomNameToList(name); + } } diff --git a/common/src/main/java/mc/duzo/fakeplayer/util/SkinGrabber.java b/common/src/main/java/mc/duzo/fakeplayer/util/SkinGrabber.java index 4451ae9..7f2f08e 100644 --- a/common/src/main/java/mc/duzo/fakeplayer/util/SkinGrabber.java +++ b/common/src/main/java/mc/duzo/fakeplayer/util/SkinGrabber.java @@ -1,204 +1,206 @@ package mc.duzo.fakeplayer.util; +import java.awt.image.BufferedImage; +import java.io.*; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; +import java.util.HashMap; + +import javax.imageio.ImageIO; + import com.mojang.logging.LogUtils; -import mc.duzo.fakeplayer.FakePlayerMod; +import org.asynchttpclient.*; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.texture.NativeImage; import net.minecraft.client.texture.NativeImageBackedTexture; import net.minecraft.client.texture.TextureManager; +import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.util.Identifier; -import net.minecraft.entity.Entity; -import org.asynchttpclient.*; -import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.*; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; -import java.util.HashMap; +import mc.duzo.fakeplayer.FakePlayerMod; public class SkinGrabber { - private static final Logger LOGGER = LogUtils.getLogger(); - public static final String URL = "https://mineskin.eu/skin/"; - public static final String DEFAULT_DIR = "./fakeplayers/skins/"; - public static final String ERROR_SKIN = "textures/entity/humanoid/error.png"; - public static final Identifier ERROR_TEXTURE = new Identifier(FakePlayerMod.MOD_ID, ERROR_SKIN); - public static final HashMap SKIN_LIST = new HashMap<>(); - - public static Identifier getEntitySkinFromList(LivingEntity entity) { - Identifier location = SkinGrabber.getSkinFromListKey(entity.getUuidAsString()); - return location; - } - - public static String formatEntityCustomName(Entity entity) { - return entity.getName().getString().toLowerCase().replace(" ", ""); - } - - public static Identifier getEntitySkinFromFile(Entity entity) { - File file = new File(DEFAULT_DIR + formatEntityCustomName(entity) + ".png"); - Identifier location = fileToLocation(file); - return location; - } - - public static Identifier getCustomNameSkinFromFile(String name) { - File file = new File(DEFAULT_DIR + name.toLowerCase().replace(" ", "") + ".png"); - Identifier location = fileToLocation(file); - return location; - } - - public static void addEntityToList(Entity entity) { - Identifier location = SkinGrabber.getEntitySkinFromFile(entity); + private static final Logger LOGGER = LogUtils.getLogger(); + public static final String URL = "https://mineskin.eu/skin/"; + public static final String DEFAULT_DIR = "./fakeplayers/skins/"; + public static final String ERROR_SKIN = "textures/entity/humanoid/error.png"; + public static final Identifier ERROR_TEXTURE = new Identifier(FakePlayerMod.MOD_ID, ERROR_SKIN); + public static final HashMap SKIN_LIST = new HashMap<>(); + + public static Identifier getEntitySkinFromList(LivingEntity entity) { + Identifier location = SkinGrabber.getSkinFromListKey(entity.getUuidAsString()); + return location; + } + + public static String formatEntityCustomName(Entity entity) { + return entity.getName().getString().toLowerCase().replace(" ", ""); + } + + public static Identifier getEntitySkinFromFile(Entity entity) { + File file = new File(DEFAULT_DIR + formatEntityCustomName(entity) + ".png"); + Identifier location = fileToLocation(file); + return location; + } + + public static Identifier getCustomNameSkinFromFile(String name) { + File file = new File(DEFAULT_DIR + name.toLowerCase().replace(" ", "") + ".png"); + Identifier location = fileToLocation(file); + return location; + } + + public static void addEntityToList(Entity entity) { + Identifier location = SkinGrabber.getEntitySkinFromFile(entity); // System.out.println("Adding " + formatEntityCustomName(entity) + " " + location); - SKIN_LIST.put(formatEntityCustomName(entity),location); - } - - public static void addCustomNameToList(String name) { - Identifier location = SkinGrabber.getCustomNameSkinFromFile(name); - SKIN_LIST.put(name.toLowerCase().replace(" ", ""),location); - } - - public static void clearTextures() { - MinecraftClient minecraft = MinecraftClient.getInstance(); - if (minecraft.world == null) { - TextureManager manager = minecraft.getTextureManager(); - - // Release the textures if the cache isnt empty - if (!SkinGrabber.SKIN_LIST.isEmpty()) { - SkinGrabber.SKIN_LIST.forEach(((uuid, Identifier) -> manager.destroyTexture(Identifier))); - SkinGrabber.SKIN_LIST.clear(); - } - } - } - - public static Identifier getSkinFromListKey(String key) { - if (SkinGrabber.SKIN_LIST.containsKey(key)) { - return SkinGrabber.SKIN_LIST.get(key); - } - return null; - } - - public static Identifier fileToLocation(File file) { - NativeImage image = null; - try { - image = processLegacySkin(NativeImage.read(new FileInputStream(file))); - } catch (IOException e) { - e.printStackTrace(); - } - if (image == null) { - return new Identifier(FakePlayerMod.MOD_ID,ERROR_SKIN); - } - return registerImage(image); - } - - public static Identifier registerImage(NativeImage image) { - TextureManager manager = MinecraftClient.getInstance().getTextureManager(); - return manager.registerDynamicTexture("humanoid", new NativeImageBackedTexture(image)); - } - - public static boolean isValidURL(String url) { - try { - new URL(url).toURI(); - return true; - } catch (MalformedURLException | URISyntaxException e) { - return false; - } - } - - public static void downloadImageFromURL(String filename,File filepath, String URL) { - try { - URL url = new URL(URL); - URLConnection connection = url.openConnection(); - connection.connect(); - connection.setConnectTimeout(0); - - BufferedImage image = ImageIO.read(connection.getInputStream()); - - if (!filepath.exists()) { - filepath.mkdirs(); - } - - ImageIO.write(image, "png", new File(filepath, filename + ".png")); - System.out.println(image); - - } catch (Exception exception) { - exception.printStackTrace(); - } - } - - public static void downloadSkinFromUsername(String username, File filepath) { - downloadImageFromURL(username, filepath, SkinGrabber.URL + username); - } - - // These functions were coped from HttpTexture by Mojang (thak you moywang( - @Nullable - private static NativeImage processLegacySkin(NativeImage image) { - int i = image.getHeight(); - int j = image.getWidth(); - if (j == 64 && (i == 32 || i == 64)) { - boolean flag = i == 32; - if (flag) { - NativeImage nativeimage = new NativeImage(64, 64, true); - nativeimage.copyFrom(image); - image.close(); - image = nativeimage; - nativeimage.fillRect(0, 32, 64, 32, 0); - nativeimage.copyRect(4, 16, 16, 32, 4, 4, true, false); - nativeimage.copyRect(8, 16, 16, 32, 4, 4, true, false); - nativeimage.copyRect(0, 20, 24, 32, 4, 12, true, false); - nativeimage.copyRect(4, 20, 16, 32, 4, 12, true, false); - nativeimage.copyRect(8, 20, 8, 32, 4, 12, true, false); - nativeimage.copyRect(12, 20, 16, 32, 4, 12, true, false); - nativeimage.copyRect(44, 16, -8, 32, 4, 4, true, false); - nativeimage.copyRect(48, 16, -8, 32, 4, 4, true, false); - nativeimage.copyRect(40, 20, 0, 32, 4, 12, true, false); - nativeimage.copyRect(44, 20, -8, 32, 4, 12, true, false); - nativeimage.copyRect(48, 20, -16, 32, 4, 12, true, false); - nativeimage.copyRect(52, 20, -8, 32, 4, 12, true, false); - } - - setNoAlpha(image, 0, 0, 32, 16); - if (flag) { - doNotchTransparencyHack(image, 32, 0, 64, 32); - } - - setNoAlpha(image, 0, 16, 64, 32); - setNoAlpha(image, 16, 48, 48, 64); - return image; - } else { - image.close(); - LOGGER.warn("Discarding incorrectly sized ({}x{}) skin texture", j, i); - return null; - } - } - - private static void doNotchTransparencyHack(NativeImage p_118013_, int p_118014_, int p_118015_, int p_118016_, int p_118017_) { - for(int i = p_118014_; i < p_118016_; ++i) { - for(int j = p_118015_; j < p_118017_; ++j) { - int k = p_118013_.getColor(i, j); - if ((k >> 24 & 255) < 128) { - return; - } - } - } - - for(int l = p_118014_; l < p_118016_; ++l) { - for(int i1 = p_118015_; i1 < p_118017_; ++i1) { - p_118013_.setColor(l, i1, p_118013_.getColor(l, i1) & 16777215); - } - } - - } - - private static void setNoAlpha(NativeImage p_118023_, int p_118024_, int p_118025_, int p_118026_, int p_118027_) { - for (int i = p_118024_; i < p_118026_; ++i) { - for (int j = p_118025_; j < p_118027_; ++j) { - p_118023_.setColor(i, j, p_118023_.getColor(i, j) | -16777216); - } - } - } + SKIN_LIST.put(formatEntityCustomName(entity),location); + } + + public static void addCustomNameToList(String name) { + Identifier location = SkinGrabber.getCustomNameSkinFromFile(name); + SKIN_LIST.put(name.toLowerCase().replace(" ", ""),location); + } + + public static void clearTextures() { + MinecraftClient minecraft = MinecraftClient.getInstance(); + if (minecraft.world == null) { + TextureManager manager = minecraft.getTextureManager(); + + // Release the textures if the cache isnt empty + if (!SkinGrabber.SKIN_LIST.isEmpty()) { + SkinGrabber.SKIN_LIST.forEach(((uuid, Identifier) -> manager.destroyTexture(Identifier))); + SkinGrabber.SKIN_LIST.clear(); + } + } + } + + public static Identifier getSkinFromListKey(String key) { + if (SkinGrabber.SKIN_LIST.containsKey(key)) { + return SkinGrabber.SKIN_LIST.get(key); + } + return null; + } + + public static Identifier fileToLocation(File file) { + NativeImage image = null; + try { + image = processLegacySkin(NativeImage.read(new FileInputStream(file))); + } catch (IOException e) { + e.printStackTrace(); + } + if (image == null) { + return new Identifier(FakePlayerMod.MOD_ID,ERROR_SKIN); + } + return registerImage(image); + } + + public static Identifier registerImage(NativeImage image) { + TextureManager manager = MinecraftClient.getInstance().getTextureManager(); + return manager.registerDynamicTexture("humanoid", new NativeImageBackedTexture(image)); + } + + public static boolean isValidURL(String url) { + try { + new URL(url).toURI(); + return true; + } catch (MalformedURLException | URISyntaxException e) { + return false; + } + } + + public static void downloadImageFromURL(String filename,File filepath, String URL) { + try { + URL url = new URL(URL); + URLConnection connection = url.openConnection(); + connection.connect(); + connection.setConnectTimeout(0); + + BufferedImage image = ImageIO.read(connection.getInputStream()); + + if (!filepath.exists()) { + filepath.mkdirs(); + } + + ImageIO.write(image, "png", new File(filepath, filename + ".png")); + System.out.println(image); + + } catch (Exception exception) { + exception.printStackTrace(); + } + } + + public static void downloadSkinFromUsername(String username, File filepath) { + downloadImageFromURL(username, filepath, SkinGrabber.URL + username); + } + + // These functions were coped from HttpTexture by Mojang (thak you moywang( + @Nullable private static NativeImage processLegacySkin(NativeImage image) { + int i = image.getHeight(); + int j = image.getWidth(); + if (j == 64 && (i == 32 || i == 64)) { + boolean flag = i == 32; + if (flag) { + NativeImage nativeimage = new NativeImage(64, 64, true); + nativeimage.copyFrom(image); + image.close(); + image = nativeimage; + nativeimage.fillRect(0, 32, 64, 32, 0); + nativeimage.copyRect(4, 16, 16, 32, 4, 4, true, false); + nativeimage.copyRect(8, 16, 16, 32, 4, 4, true, false); + nativeimage.copyRect(0, 20, 24, 32, 4, 12, true, false); + nativeimage.copyRect(4, 20, 16, 32, 4, 12, true, false); + nativeimage.copyRect(8, 20, 8, 32, 4, 12, true, false); + nativeimage.copyRect(12, 20, 16, 32, 4, 12, true, false); + nativeimage.copyRect(44, 16, -8, 32, 4, 4, true, false); + nativeimage.copyRect(48, 16, -8, 32, 4, 4, true, false); + nativeimage.copyRect(40, 20, 0, 32, 4, 12, true, false); + nativeimage.copyRect(44, 20, -8, 32, 4, 12, true, false); + nativeimage.copyRect(48, 20, -16, 32, 4, 12, true, false); + nativeimage.copyRect(52, 20, -8, 32, 4, 12, true, false); + } + + setNoAlpha(image, 0, 0, 32, 16); + if (flag) { + doNotchTransparencyHack(image, 32, 0, 64, 32); + } + + setNoAlpha(image, 0, 16, 64, 32); + setNoAlpha(image, 16, 48, 48, 64); + return image; + } else { + image.close(); + LOGGER.warn("Discarding incorrectly sized ({}x{}) skin texture", j, i); + return null; + } + } + + private static void doNotchTransparencyHack(NativeImage p_118013_, int p_118014_, int p_118015_, int p_118016_, int p_118017_) { + for(int i = p_118014_; i < p_118016_; ++i) { + for(int j = p_118015_; j < p_118017_; ++j) { + int k = p_118013_.getColor(i, j); + if ((k >> 24 & 255) < 128) { + return; + } + } + } + + for(int l = p_118014_; l < p_118016_; ++l) { + for(int i1 = p_118015_; i1 < p_118017_; ++i1) { + p_118013_.setColor(l, i1, p_118013_.getColor(l, i1) & 16777215); + } + } + + } + + private static void setNoAlpha(NativeImage p_118023_, int p_118024_, int p_118025_, int p_118026_, int p_118027_) { + for (int i = p_118024_; i < p_118026_; ++i) { + for (int j = p_118025_; j < p_118027_; ++j) { + p_118023_.setColor(i, j, p_118023_.getColor(i, j) | -16777216); + } + } + } } diff --git a/versioning.gradle b/versioning.gradle index 265fb8b..f694676 100644 --- a/versioning.gradle +++ b/versioning.gradle @@ -1,6 +1,6 @@ ext.getCurrentVersion = { - var patch = System.getenv("GITHUB_RUN_NUMBER") + var patch = System.getenv("GITHUB_RUN_NUMBER") - return project.mod_version + (patch != null ? ("." + System.getenv("GITHUB_RUN_NUMBER")) : "")\ + return project.mod_version + (patch != null ? ("." + System.getenv("GITHUB_RUN_NUMBER")) : "")\ + "-" + project.minecraft_version + "-" + project.mod_version_qualifier -} \ No newline at end of file +}