From e5f7a15895a60a3f7dff878a057e3dc732842c39 Mon Sep 17 00:00:00 2001 From: Pedro Date: Mon, 22 Jan 2024 21:12:27 -0300 Subject: [PATCH] Fixed metadata and block entities being applied wrong --- build.gradle | 24 ++++++++++++++++--- .../commander/CommanderHelper.java | 20 ++++++++++++++++ .../content/commands/CloneCommand.java | 8 +++++-- .../content/commands/FillCommand.java | 20 +++++++++++----- .../content/commands/SetBlockCommand.java | 14 ++++------- 5 files changed, 65 insertions(+), 21 deletions(-) diff --git a/build.gradle b/build.gradle index 14b416c..3706013 100644 --- a/build.gradle +++ b/build.gradle @@ -78,13 +78,29 @@ repositories { metadataSources { artifact() } } ivy { - url = "https://github.com/ToufouMaster" + url = "https://github.com/Pedro270707" patternLayout { - artifact "[organisation]/releases/download/[revision]/[module]-[revision].jar" + artifact "[organisation]/releases/download/v[revision]/[module]-[revision].jar" + m2compatible = true + } + metadataSources { artifact() } + } + ivy { + url = "https://github.com/UselessBullets" + patternLayout { + artifact "[organisation]/releases/download/v[revision]/[module]-[revision].jar" m2compatible = true } metadataSources { artifact() } } +// ivy { +// url = "https://github.com/ToufouMaster" +// patternLayout { +// artifact "[organisation]/releases/download/[revision]/[module]-[revision].jar" +// m2compatible = true +// } +// metadataSources { artifact() } +// } } dependencies { @@ -98,8 +114,10 @@ dependencies { // If you do not need Halplibe you can comment this line out or delete this line modImplementation "com.github.Turnip-Labs:bta-halplibe:${project.halplibe_version}" - modImplementation "BTWaila:btwaila:1.0.8-7.1" +// modImplementation "BTWaila:btwaila:1.0.8-7.1" modImplementation "ModMenu:ModMenu:2.0.0" + modImplementation "command-blocks-bta:commandblocks:1.0.0" + modImplementation "DragonFly:dragonfly:1.4.1-7.1" include modImplementation('com.mojang:brigadier:1.0.18') diff --git a/src/main/java/net/pedroricardo/commander/CommanderHelper.java b/src/main/java/net/pedroricardo/commander/CommanderHelper.java index afc9460..abf81f4 100644 --- a/src/main/java/net/pedroricardo/commander/CommanderHelper.java +++ b/src/main/java/net/pedroricardo/commander/CommanderHelper.java @@ -8,11 +8,13 @@ import com.mojang.nbt.CompoundTag; import com.mojang.nbt.Tag; import net.minecraft.core.block.Block; +import net.minecraft.core.block.entity.TileEntity; import net.minecraft.core.entity.Entity; import net.minecraft.core.entity.EntityDispatcher; import net.minecraft.core.entity.EntityLiving; import net.minecraft.core.util.helper.LogPrintStream; import net.minecraft.core.util.helper.MathHelper; +import net.minecraft.core.world.WorldSource; import net.minecraft.core.world.generate.feature.WorldFeature; import net.pedroricardo.commander.content.CommanderCommandSource; import net.pedroricardo.commander.content.helpers.IntegerCoordinates; @@ -132,6 +134,24 @@ public static boolean blockEntitiesAreEqual(CompoundTag first, CompoundTag secon return true; } + public static void setTileEntity(WorldSource world, int x, int y, int z, CompoundTag tag) { + if (tag == null || world.getBlockTileEntity(x, y, z) == null) return; + tag.putInt("x", x); + tag.putInt("y", y); + tag.putInt("z", z); + world.getBlockTileEntity(x, y, z).readFromNBT(tag); + } + + public static void setTileEntity(WorldSource world, int x, int y, int z, TileEntity tileEntity) { + setTileEntity(world, x, y, z, tagFrom(tileEntity)); + } + + public static CompoundTag tagFrom(TileEntity tileEntity) { + CompoundTag tag = new CompoundTag(); + tileEntity.writeToNBT(tag); + return tag; + } + public static void init() { try { for (Class clazz : CommanderReflectionHelper.getAllClasses(className -> className.startsWith("net.minecraft.core.world.generate.feature"))) { diff --git a/src/main/java/net/pedroricardo/commander/content/commands/CloneCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/CloneCommand.java index dc1cc97..1db99d0 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/CloneCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/CloneCommand.java @@ -9,6 +9,7 @@ import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.nbt.CompoundTag; import net.minecraft.core.block.entity.TileEntity; +import net.minecraft.core.data.tag.Tag; import net.minecraft.core.lang.I18n; import net.minecraft.core.util.phys.AABB; import net.minecraft.core.util.phys.Vec3d; @@ -174,8 +175,11 @@ public static int clone(CommanderCommandSource source, WorldAndPosition start, W if (tileEntity != null) tileEntity.writeToNBT(blockTag); if (filter == null || (destination.getWorld().getBlockId(offsetDestinationX, offsetDestinationY, offsetDestinationZ) == filter.getBlockId() && destination.getWorld().getBlockMetadata(offsetDestinationX, offsetDestinationY, offsetDestinationZ) == filter.getMetadata() && (filter.getTag().getValues().isEmpty() || CommanderHelper.blockEntitiesAreEqual(blockTag, filter.getTag())))) { if (destination.getWorld().getBlockId(offsetDestinationX, offsetDestinationY, offsetDestinationZ) != entry.getValue().getBlockId() || destination.getWorld().getBlockMetadata(offsetDestinationX, offsetDestinationY, offsetDestinationZ) != entry.getValue().getMetadata()) ++clonedBlocks; - destination.getWorld().setBlockAndMetadataWithNotify(offsetDestinationX, offsetDestinationY, offsetDestinationZ, entry.getValue().getBlockId(), entry.getValue().getMetadata()); - if (entry.getValue().getTileEntity() != null) destination.getWorld().setBlockTileEntity(offsetDestinationX, offsetDestinationY, offsetDestinationZ, entry.getValue().getTileEntity()); + destination.getWorld().setBlockWithNotify(offsetDestinationX, offsetDestinationY, offsetDestinationZ, entry.getValue().getBlockId()); + destination.getWorld().setBlockMetadataWithNotify(offsetDestinationX, offsetDestinationY, offsetDestinationZ, entry.getValue().getMetadata()); + if (entry.getValue().getTileEntity() != null) { + CommanderHelper.setTileEntity(destination.getWorld(), offsetDestinationX, offsetDestinationY, offsetDestinationZ, entry.getValue().getTileEntity()); + } } } destination.getWorld().editingBlocks = false; diff --git a/src/main/java/net/pedroricardo/commander/content/commands/FillCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/FillCommand.java index b704d25..40e5af0 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/FillCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/FillCommand.java @@ -120,11 +120,14 @@ public static int fillReplace(CommanderCommandSource source, World world, Intege CompoundTag blockTag = new CompoundTag(); TileEntity tileEntity = world.getBlockTileEntity(x, y, z); if (tileEntity != null) tileEntity.writeToNBT(blockTag); - if ((block.getBlockId() != world.getBlockId(x, y, z) || block.getMetadata() != world.getBlockMetadata(x, y, z)) && (filter == null || (world.getBlockId(x, y, z) == filter.getBlockId() && world.getBlockMetadata(x, y, z) == filter.getMetadata() && (filter.getTag().getValues().isEmpty() || CommanderHelper.blockEntitiesAreEqual(blockTag, filter.getTag()))))) { + if ((block.getBlockId() != world.getBlockId(x, y, z) || block.getMetadata() != world.getBlockMetadata(x, y, z) || !CommanderHelper.blockEntitiesAreEqual(block.getTag(), CommanderHelper.tagFrom(world.getBlockTileEntity(x, y, z)))) && (filter == null || (world.getBlockId(x, y, z) == filter.getBlockId() && world.getBlockMetadata(x, y, z) == filter.getMetadata() && (filter.getTag().getValues().isEmpty() || CommanderHelper.blockEntitiesAreEqual(blockTag, filter.getTag()))))) { ++blocksFilled; if (destroy && world.getBlock(x, y, z) != null) world.getBlock(x, y, z).getBreakResult(world, EnumDropCause.WORLD, x, y, z, world.getBlockMetadata(x, y, z), world.getBlockTileEntity(x, y, z)); } - if (filter == null || (world.getBlockId(x, y, z) == filter.getBlockId() && world.getBlockMetadata(x, y, z) == filter.getMetadata())) world.setBlockAndMetadataWithNotify(x, y, z, block.getBlockId(), block.getMetadata()); + if (filter == null || (world.getBlockId(x, y, z) == filter.getBlockId() && world.getBlockMetadata(x, y, z) == filter.getMetadata() && (!filter.getTag().getValue().isEmpty() || CommanderHelper.blockEntitiesAreEqual(CommanderHelper.tagFrom(world.getBlockTileEntity(x, y, z)), filter.getTag())))) { + world.setBlockWithNotify(x, y, z, block.getBlockId()); + world.setBlockMetadataWithNotify(x, y, z, block.getMetadata()); + } } } } @@ -147,11 +150,12 @@ public static int fillHollow(CommanderCommandSource source, World world, Integer for (int y = minY; y <= maxY; y++) { for (int z = minZ; z <= maxZ; z++) { boolean isOutline = x == minX || x == maxX || y == minY || y == maxY || z == minZ || z == maxZ; - if ((isOutline && (block.getBlockId() != world.getBlockId(x, y, z) || block.getMetadata() != world.getBlockMetadata(x, y, z))) || (!isOutline && world.getBlockId(x, y, z) != 0)) { + if ((isOutline && (block.getBlockId() != world.getBlockId(x, y, z) || block.getMetadata() != world.getBlockMetadata(x, y, z) || !CommanderHelper.blockEntitiesAreEqual(block.getTag(), CommanderHelper.tagFrom(world.getBlockTileEntity(x, y, z))))) || (!isOutline && world.getBlockId(x, y, z) != 0)) { ++blocksFilled; if (!isOutline && world.getBlock(x, y, z) != null) world.getBlock(x, y, z).getBreakResult(world, EnumDropCause.WORLD, x, y, z, world.getBlockMetadata(x, y, z), world.getBlockTileEntity(x, y, z)); } - world.setBlockAndMetadataWithNotify(x, y, z, isOutline ? block.getBlockId() : 0, isOutline ? block.getMetadata() : 0); + world.setBlockWithNotify(x, y, z, isOutline ? block.getBlockId() : 0); + world.setBlockMetadataWithNotify(x, y, z, isOutline ? block.getMetadata() : 0); } } } @@ -174,8 +178,12 @@ public static int fillOutline(CommanderCommandSource source, World world, Intege for (int y = minY; y <= maxY; y++) { for (int z = minZ; z <= maxZ; z++) { boolean isOutline = x == minX || x == maxX || y == minY || y == maxY || z == minZ || z == maxZ; - if (isOutline && (block.getBlockId() != world.getBlockId(x, y, z) || block.getMetadata() != world.getBlockMetadata(x, y, z))) ++blocksFilled; - if (isOutline) world.setBlockAndMetadataWithNotify(x, y, z, block.getBlockId(), block.getMetadata()); + if (isOutline && (block.getBlockId() != world.getBlockId(x, y, z) || block.getMetadata() != world.getBlockMetadata(x, y, z) || !CommanderHelper.blockEntitiesAreEqual(block.getTag(), CommanderHelper.tagFrom(world.getBlockTileEntity(x, y, z))))) ++blocksFilled; + if (isOutline) { + world.setBlockWithNotify(x, y, z, block.getBlockId()); + world.setBlockMetadataWithNotify(x, y, z, block.getMetadata()); + CommanderHelper.setTileEntity(world, x, y, z, block.getTag()); + } } } } diff --git a/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java index 3bfdcff..da27ee5 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java @@ -10,6 +10,7 @@ import net.minecraft.core.block.entity.TileEntity; import net.minecraft.core.lang.I18n; import net.minecraft.core.world.World; +import net.pedroricardo.commander.CommanderHelper; import net.pedroricardo.commander.content.CommanderCommandSource; import net.pedroricardo.commander.content.arguments.*; import net.pedroricardo.commander.content.helpers.BlockInput; @@ -38,16 +39,9 @@ public static void register(CommandDispatcher dispatcher if (!world.isBlockLoaded(x, y, z)) { throw FAILURE.create(); } else { - world.setBlockAndMetadataWithNotify(x, y, z, blockInput.getBlockId(), blockInput.getMetadata()); - TileEntity tileEntity = source.getWorld().getBlockTileEntity(x, y, z); - if (tileEntity != null) { - CompoundTag tag = new CompoundTag(); - tileEntity.writeToNBT(tag); - for (Map.Entry> entry : blockInput.getTag().getValue().entrySet()) { - tag.put(entry.getKey(), entry.getValue()); - } - tileEntity.readFromNBT(tag); - } + world.setBlockWithNotify(x, y, z, blockInput.getBlockId()); + world.setBlockMetadataWithNotify(x, y, z, blockInput.getMetadata()); + CommanderHelper.setTileEntity(world, x, y, z, blockInput.getTag()); source.sendTranslatableMessage("commands.commander.setblock.success", coordinates.getX(source), coordinates.getY(source, true), coordinates.getZ(source)); }