Skip to content

Commit

Permalink
Fixed metadata and block entities being applied wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Jan 23, 2024
1 parent 685f015 commit e5f7a15
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
24 changes: 21 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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')

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/pedroricardo/commander/CommanderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -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());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,16 +39,9 @@ public static void register(CommandDispatcher<CommanderCommandSource> 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<String, Tag<?>> 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));
}

Expand Down

0 comments on commit e5f7a15

Please sign in to comment.