diff --git a/gradle.properties b/gradle.properties index 0c448461c..99a357d9d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -26,10 +26,8 @@ additionalMinecraftVersions= mappingsChannel=official mappingsVersion=1.20.1 - blockUiVersion=1.20.1-1.0.156-RELEASE blockUiRange=[1.20.1-0.0.98-ALPHA,) - domumOrnamentumVersion=1.20.1-1.0.199-BETA domumOrnamentumRange=[1.20-1.0.93-ALPHA,) diff --git a/src/main/java/com/ldtteam/structurize/placement/handlers/placement/DoBlockPlacementHandler.java b/src/main/java/com/ldtteam/structurize/placement/handlers/placement/DoBlockPlacementHandler.java new file mode 100644 index 000000000..75f47bd66 --- /dev/null +++ b/src/main/java/com/ldtteam/structurize/placement/handlers/placement/DoBlockPlacementHandler.java @@ -0,0 +1,154 @@ +package com.ldtteam.structurize.placement.handlers.placement; + +import com.ldtteam.domumornamentum.block.IMateriallyTexturedBlock; +import com.ldtteam.domumornamentum.block.decorative.PillarBlock; +import com.ldtteam.domumornamentum.util.BlockUtils; +import com.ldtteam.structurize.api.util.ItemStackUtils; +import com.ldtteam.structurize.api.util.Log; +import com.ldtteam.structurize.placement.structure.IStructureHandler; +import com.ldtteam.structurize.util.InventoryUtils; +import com.ldtteam.structurize.util.PlacementSettings; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.context.BlockPlaceContext; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.FenceBlock; +import net.minecraft.world.level.block.IronBarsBlock; +import net.minecraft.world.level.block.WallBlock; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.Vec3; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static com.ldtteam.structurize.api.util.constant.Constants.UPDATE_FLAG; +import static com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers.handleTileEntityPlacement; + +public class DoBlockPlacementHandler implements IPlacementHandler +{ + @Override + public boolean canHandle(@NotNull final Level world, @NotNull final BlockPos pos, @NotNull final BlockState blockState) + { + return blockState.getBlock() instanceof IMateriallyTexturedBlock; + } + + @Override + public ActionProcessingResult handle( + @NotNull final Level world, + @NotNull final BlockPos pos, + @NotNull final BlockState blockState, + @Nullable final CompoundTag tileEntityData, + final boolean complete, + final BlockPos centerPos, + final PlacementSettings settings) + { + BlockState placementState = blockState; + if (blockState.getBlock() instanceof WallBlock || blockState.getBlock() instanceof FenceBlock || blockState.getBlock() instanceof PillarBlock + || blockState.getBlock() instanceof IronBarsBlock) + { + try + { + final BlockState tempState = blockState.getBlock().getStateForPlacement( + new BlockPlaceContext(world, null, InteractionHand.MAIN_HAND, ItemStack.EMPTY, + new BlockHitResult(new Vec3(0, 0, 0), Direction.DOWN, pos, true))); + if (tempState != null) + { + placementState = tempState; + } + } + catch (final Exception ex) + { + // Noop + } + } + + if (world.getBlockState(pos).equals(placementState)) + { + world.removeBlock(pos, false); + world.setBlock(pos, placementState, UPDATE_FLAG); + if (tileEntityData != null) + { + try + { + handleTileEntityPlacement(tileEntityData, world, pos, settings); + placementState.getBlock().setPlacedBy(world, pos, placementState, null, placementState.getBlock().getCloneItemStack(placementState, + new BlockHitResult(new Vec3(0, 0, 0), Direction.NORTH, pos, false), world, pos, null)); + } + catch (final Exception ex) + { + Log.getLogger().warn("Unable to place TileEntity"); + } + } + return ActionProcessingResult.PASS; + } + + if (!world.setBlock(pos, placementState, UPDATE_FLAG)) + { + return ActionProcessingResult.PASS; + } + + if (tileEntityData != null) + { + try + { + handleTileEntityPlacement(tileEntityData, world, pos, settings); + blockState.getBlock().setPlacedBy(world, pos, placementState, null, placementState.getBlock().getCloneItemStack(placementState, + new BlockHitResult(new Vec3(0, 0, 0), Direction.NORTH, pos, false), world, pos, null)); + } + catch (final Exception ex) + { + Log.getLogger().warn("Unable to place TileEntity"); + } + } + + return ActionProcessingResult.SUCCESS; + } + + @Override + public List getRequiredItems( + @NotNull final Level world, + @NotNull final BlockPos pos, + @NotNull final BlockState blockState, + @Nullable final CompoundTag tileEntityData, + final boolean complete) + { + final List itemList = new ArrayList<>(); + if (tileEntityData != null) + { + BlockPos blockpos = new BlockPos(tileEntityData.getInt("x"), tileEntityData.getInt("y"), tileEntityData.getInt("z")); + final BlockEntity tileEntity = BlockEntity.loadStatic(blockpos, blockState, tileEntityData); + if (tileEntity == null) + { + return Collections.emptyList(); + } + itemList.add(BlockUtils.getMaterializedItemStack(null, tileEntity)); + } + itemList.removeIf(ItemStackUtils::isEmpty); + return itemList; + } + + @Override + public void handleRemoval( + final IStructureHandler handler, + final Level world, + final BlockPos pos) + { + if (!handler.isCreative()) + { + final List items = com.ldtteam.structurize.util.BlockUtils.getBlockDrops(world, pos, 0, handler.getHeldItem()); + for (final ItemStack item : items) + { + InventoryUtils.transferIntoNextBestSlot(item, handler.getInventory()); + } + } + world.removeBlock(pos, false); + } +} diff --git a/src/main/java/com/ldtteam/structurize/placement/handlers/placement/PlacementHandlers.java b/src/main/java/com/ldtteam/structurize/placement/handlers/placement/PlacementHandlers.java index 4cbd20358..0ad3c999b 100644 --- a/src/main/java/com/ldtteam/structurize/placement/handlers/placement/PlacementHandlers.java +++ b/src/main/java/com/ldtteam/structurize/placement/handlers/placement/PlacementHandlers.java @@ -65,6 +65,7 @@ public final class PlacementHandlers handlers.add(new DripStoneBlockPlacementHandler()); handlers.add(new FallingBlockPlacementHandler()); handlers.add(new BannerPlacementHandler()); + handlers.add(new DoBlockPlacementHandler()); handlers.add(new GeneralBlockPlacementHandler()); } diff --git a/src/main/java/com/ldtteam/structurize/util/ChangeStorage.java b/src/main/java/com/ldtteam/structurize/util/ChangeStorage.java index 270de5ce6..1cfd9acf1 100644 --- a/src/main/java/com/ldtteam/structurize/util/ChangeStorage.java +++ b/src/main/java/com/ldtteam/structurize/util/ChangeStorage.java @@ -1,6 +1,7 @@ package com.ldtteam.structurize.util; import com.ldtteam.structurize.Structurize; +import com.ldtteam.structurize.api.util.constant.Constants; import com.ldtteam.structurize.management.Manager; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; @@ -8,6 +9,8 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; import org.jetbrains.annotations.Nullable; import java.util.*; @@ -138,12 +141,14 @@ public boolean undo(final Level world, @Nullable final ChangeStorage undoStorage { undoStorage.addPreviousDataFor(entry.getKey(), world); } - world.setBlockAndUpdate(entry.getKey(), entry.getValue().getPreState()); + world.setBlock(entry.getKey(), Blocks.COBBLESTONE.defaultBlockState(), Block.UPDATE_CLIENTS); + world.setBlock(entry.getKey(), entry.getValue().getPreState(), Constants.UPDATE_FLAG); if (entry.getValue().getPreTE() != null) { world.setBlockEntity(entry.getValue().getPreTE()); } + world.markAndNotifyBlock(entry.getKey(), world.getChunkAt(entry.getKey()), entry.getValue().getPreState(), entry.getValue().getPreState(), 2, 512); if (undoStorage != null) { @@ -202,16 +207,19 @@ public boolean redo(final Level world) while (iterator.hasNext()) { final Map.Entry entry = iterator.next(); - if (world.getBlockState(entry.getKey()) != entry.getValue().getPreState()) + if (world.getBlockState(entry.getKey()).getBlock() != entry.getValue().getPreState().getBlock()) { continue; } - world.setBlockAndUpdate(entry.getKey(), entry.getValue().getPostState()); + world.setBlock(entry.getKey(), Blocks.COBBLESTONE.defaultBlockState(), Block.UPDATE_CLIENTS); + world.setBlock(entry.getKey(), entry.getValue().getPostState(), Constants.UPDATE_FLAG); if (entry.getValue().getPostTE() != null) { world.setBlockEntity(entry.getValue().getPostTE()); } + world.markAndNotifyBlock(entry.getKey(), world.getChunkAt(entry.getKey()), entry.getValue().getPostState(), entry.getValue().getPostState(), 2, 512); + count++; if (count >= Structurize.getConfig().getServer().maxOperationsPerTick.get())