From 0d4fabaf6d101594e53a17dac6fe5879c8c1e305 Mon Sep 17 00:00:00 2001 From: QuImUfu Date: Thu, 1 Apr 2021 11:56:02 +0200 Subject: [PATCH] fixed player deletion bug, added options for entity (dis)placement --- README.md | 4 +- .../java/quimufu/structure_item/MyItem.java | 29 +++++++++-- .../structure_item/MyPlacementSettings.java | 49 +++++++++++++------ .../assets/structure_item/lang/en_us.json | 8 ++- 4 files changed, 69 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8bcb4a3..ca0f891 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Currently, the only supported version of the mod ## Example Usage ``` -/give User structure_item:item{offset: [I; 0, 5, 0],structure: "structure:mine",allowedOn: "minecraft:stone",blacklist:["minecraft:bedrock"]} 1 +/give User structure_item:item{offset: [I; 0, 5, 0],structure: "structure:mine",allowedOn: "minecraft:stone",blacklist:["minecraft:bedrock"], replaceEntities:0, placeEntities:0} 1 ``` And tada, you have an item capable of placing structures on right click. If the clicked block is Stone and neither Diamond blocks nor Bedrock is in the way, it will place the structure "structure:mine" moved up by 5 blocks. (The low coordinate corner will start 5 blocks above the block you would have placed if this item were a block). Blocks (non structure voids) delete intersecting entities. @@ -12,6 +12,8 @@ If you leave out "structure", it won't work. If you leave out "offset", it will place the structure at the block you clicked at, expanding in your view direction, up and to both sides. if you look up or down, it'll place the structure centered above or below the block you clicked at. If you leave out "blacklist", it will replace anything. If you leave out "allowedOn", it will allow "placement" on any block. +If you leave out "replaceEntities" or set it to 1, entities in the way will be deleted. +If you leave out "placeEntities" or set it to 1, entities saved in the structure will be placed. Any errors made by the creators will be displayed in chat, any errors made by the user will produce a fat massage in the middle of the screen. diff --git a/src/main/java/quimufu/structure_item/MyItem.java b/src/main/java/quimufu/structure_item/MyItem.java index e56cb6e..6162c8e 100644 --- a/src/main/java/quimufu/structure_item/MyItem.java +++ b/src/main/java/quimufu/structure_item/MyItem.java @@ -7,8 +7,10 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsageContext; -import net.minecraft.nbt.*; -import net.minecraft.network.MessageType; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.NbtHelper; +import net.minecraft.nbt.Tag; import net.minecraft.network.packet.s2c.play.TitleS2CPacket; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; @@ -73,6 +75,16 @@ public class MyItem extends Item { } } } + if (!tag.contains("replaceEntities", 99) || tag.getBoolean("replaceEntities")) { + texts.add(new TranslatableText("item.structure_item.item.tooltip.replaceEntities")); + } else { + texts.add(new TranslatableText("item.structure_item.item.tooltip.doNotReplaceEntities")); + } + if (!tag.contains("placeEntities", 99) || tag.getBoolean("placeEntities")) { + texts.add(new TranslatableText("item.structure_item.item.tooltip.placeEntities")); + } else { + texts.add(new TranslatableText("item.structure_item.item.tooltip.doNotPlaceEntities")); + } } } } @@ -153,6 +165,12 @@ public class MyItem extends Item { } MyPlacementSettings ps = (new MyPlacementSettings()); + if (tag.contains("replaceEntities", 99)) { + ps.setReplaceEntities(tag.getBoolean("replaceEntities")); + } + if (tag.contains("placeEntities", 99)) { + ps.setIgnoreEntities(!tag.getBoolean("placeEntities")); + } if (tag.contains("blacklist", 9)) { ListTag bl = tag.getList("blacklist", 8); List blacklist = Lists.newArrayList(); @@ -175,7 +193,12 @@ public class MyItem extends Item { .setMirror(BlockMirror.NONE) .setRotation(BlockRotation.NONE) .setChunkPosition(null); - boolean success = x.place((ServerWorld)c.getWorld(), loc, loc, ps, c.getWorld().getRandom(), 2); + boolean success = false; + try { + if(x.place((ServerWorld)c.getWorld(), loc, loc, ps, c.getWorld().getRandom(), 2)) + success = true; + } catch (NullPointerException ignored) { + } if (success) { c.getStack().decrement(1); return ActionResult.SUCCESS; diff --git a/src/main/java/quimufu/structure_item/MyPlacementSettings.java b/src/main/java/quimufu/structure_item/MyPlacementSettings.java index 95dd3f9..dcf6ed8 100644 --- a/src/main/java/quimufu/structure_item/MyPlacementSettings.java +++ b/src/main/java/quimufu/structure_item/MyPlacementSettings.java @@ -3,6 +3,7 @@ package quimufu.structure_item; import com.google.common.collect.Lists; import net.minecraft.block.Block; import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.structure.Structure; import net.minecraft.structure.StructurePlacementData; import net.minecraft.util.math.BlockPos; @@ -16,6 +17,7 @@ public class MyPlacementSettings extends StructurePlacementData { private List blacklist; private World world; private BlockPos size; + private boolean replaceEntities = true; public void forbidOverwrite(List blocks) { if (blocks.size() == 0) { @@ -41,18 +43,10 @@ public class MyPlacementSettings extends StructurePlacementData { return super.getRandomBlockInfos(blocks, pos); } - List eligibleStructures = new ArrayList<>(); - if (blacklist == null) { - eligibleStructures = blocks; - } else { - for (Structure.PalettedBlockInfoList struct : blocks) { - if (isValid(struct, pos)) { - eligibleStructures.add(struct); - } - } - } + List eligibleStructures; + eligibleStructures = getEligibleStructures(blocks, pos); if (eligibleStructures.size() == 0) - setIgnoreEntities(true); + return null; Structure.PalettedBlockInfoList randomBlockInfos = super.getRandomBlockInfos(eligibleStructures, pos); List locs = randomBlockInfos.getAll(); if (!locs.isEmpty()) { @@ -60,7 +54,7 @@ public class MyPlacementSettings extends StructurePlacementData { for (Structure.StructureBlockInfo blockInfo : locs) { BlockPos posToClean = blockInfo.pos.add(pos); for (Entity e : entitiesWithinAABB) { - if (e.getBoundingBox().intersects(new Box(posToClean))) { + if (!(e instanceof PlayerEntity) && e.getBoundingBox().intersects(new Box(posToClean))) { e.remove(); } } @@ -69,18 +63,43 @@ public class MyPlacementSettings extends StructurePlacementData { return randomBlockInfos; } + private List getEligibleStructures(List blocks, BlockPos pos) { + List eligibleStructures = new ArrayList<>(); + if (blacklist == null && shouldReplaceEntities()) { + eligibleStructures = blocks; + } else { + for (Structure.PalettedBlockInfoList struct : blocks) { + if (isValid(struct, pos)) { + eligibleStructures.add(struct); + } + } + } + return eligibleStructures; + } + private boolean isValid(Structure.PalettedBlockInfoList struct, BlockPos pos) { + List entitiesWithinAABB = world.getNonSpectatingEntities(shouldReplaceEntities()?PlayerEntity.class:Entity.class, new Box(pos,pos.add(size))); for (Structure.StructureBlockInfo bi : struct.getAll()) { BlockPos posToCheck = bi.pos.add(pos); if (World.isValid(posToCheck)) { - Block blockToCheck = world.getBlockState(posToCheck).getBlock(); - for (Block b : blacklist) { - if (blockToCheck.equals(b)) { + for (Entity e : entitiesWithinAABB) { + if (e.getBoundingBox().intersects(new Box(posToCheck))) { return false; } } + Block blockToCheck = world.getBlockState(posToCheck).getBlock(); + if(blacklist.contains(blockToCheck)) + return false; } } return true; } + + public boolean shouldReplaceEntities() { + return replaceEntities; + } + + public void setReplaceEntities(boolean replaceEntities) { + this.replaceEntities = replaceEntities; + } } diff --git a/src/main/resources/assets/structure_item/lang/en_us.json b/src/main/resources/assets/structure_item/lang/en_us.json index fdbef94..7df49da 100644 --- a/src/main/resources/assets/structure_item/lang/en_us.json +++ b/src/main/resources/assets/structure_item/lang/en_us.json @@ -1,6 +1,6 @@ { "items.structure.spawner.invalid.block.clicked": "Digga %s is net %s, maan.", - "items.structure.spawner.invalid.location": "An irreplaceable block is in the way!", + "items.structure.spawner.invalid.location": "Something is in the way!", "items.structure.spawner.invalid.structure.name": "Invalid structure name", "items.structure.spawner.structure.nonexistent": "Structure: %s does not exist", "items.structure.spawner.invalid.block": "Block %s invalid", @@ -14,5 +14,9 @@ "item.structure_item.item.tooltip.xyz":" x: %s y: %s z: %s", "item.structure_item.item.tooltip.dynamic.offset": "Dynamic offset", "item.structure_item.item.tooltip.blacklist": "Blacklist:", - "item.structure_item.item.tooltip.blacklist.more": " And %s more..." + "item.structure_item.item.tooltip.blacklist.more": " And %s more...", + "item.structure_item.item.tooltip.replaceEntities": "Deletes Entities in the way", + "item.structure_item.item.tooltip.doNotReplaceEntities": "Doesn't allow placement with Entities in the way", + "item.structure_item.item.tooltip.placeEntities": "Places contained Entities", + "item.structure_item.item.tooltip.doNotPlaceEntities": "Doesn't place contained Entities" }