fixed player deletion bug, added options for entity (dis)placement

This commit is contained in:
QuImUfu 2021-04-01 11:56:02 +02:00
parent ace05067c7
commit 0d4fabaf6d
4 changed files with 69 additions and 21 deletions

View File

@ -4,7 +4,7 @@ Currently, the only supported version of the mod
## Example Usage ## 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. 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. 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 "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 "blacklist", it will replace anything.
If you leave out "allowedOn", it will allow "placement" on any block. 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. 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.

View File

@ -7,8 +7,10 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext; import net.minecraft.item.ItemUsageContext;
import net.minecraft.nbt.*; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.MessageType; 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.network.packet.s2c.play.TitleS2CPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity; 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()); 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)) { if (tag.contains("blacklist", 9)) {
ListTag bl = tag.getList("blacklist", 8); ListTag bl = tag.getList("blacklist", 8);
List<Block> blacklist = Lists.newArrayList(); List<Block> blacklist = Lists.newArrayList();
@ -175,7 +193,12 @@ public class MyItem extends Item {
.setMirror(BlockMirror.NONE) .setMirror(BlockMirror.NONE)
.setRotation(BlockRotation.NONE) .setRotation(BlockRotation.NONE)
.setChunkPosition(null); .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) { if (success) {
c.getStack().decrement(1); c.getStack().decrement(1);
return ActionResult.SUCCESS; return ActionResult.SUCCESS;

View File

@ -3,6 +3,7 @@ package quimufu.structure_item;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.structure.Structure; import net.minecraft.structure.Structure;
import net.minecraft.structure.StructurePlacementData; import net.minecraft.structure.StructurePlacementData;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -16,6 +17,7 @@ public class MyPlacementSettings extends StructurePlacementData {
private List<Block> blacklist; private List<Block> blacklist;
private World world; private World world;
private BlockPos size; private BlockPos size;
private boolean replaceEntities = true;
public void forbidOverwrite(List<Block> blocks) { public void forbidOverwrite(List<Block> blocks) {
if (blocks.size() == 0) { if (blocks.size() == 0) {
@ -41,18 +43,10 @@ public class MyPlacementSettings extends StructurePlacementData {
return super.getRandomBlockInfos(blocks, pos); return super.getRandomBlockInfos(blocks, pos);
} }
List<Structure.PalettedBlockInfoList> eligibleStructures = new ArrayList<>(); List<Structure.PalettedBlockInfoList> eligibleStructures;
if (blacklist == null) { eligibleStructures = getEligibleStructures(blocks, pos);
eligibleStructures = blocks;
} else {
for (Structure.PalettedBlockInfoList struct : blocks) {
if (isValid(struct, pos)) {
eligibleStructures.add(struct);
}
}
}
if (eligibleStructures.size() == 0) if (eligibleStructures.size() == 0)
setIgnoreEntities(true); return null;
Structure.PalettedBlockInfoList randomBlockInfos = super.getRandomBlockInfos(eligibleStructures, pos); Structure.PalettedBlockInfoList randomBlockInfos = super.getRandomBlockInfos(eligibleStructures, pos);
List<Structure.StructureBlockInfo> locs = randomBlockInfos.getAll(); List<Structure.StructureBlockInfo> locs = randomBlockInfos.getAll();
if (!locs.isEmpty()) { if (!locs.isEmpty()) {
@ -60,7 +54,7 @@ public class MyPlacementSettings extends StructurePlacementData {
for (Structure.StructureBlockInfo blockInfo : locs) { for (Structure.StructureBlockInfo blockInfo : locs) {
BlockPos posToClean = blockInfo.pos.add(pos); BlockPos posToClean = blockInfo.pos.add(pos);
for (Entity e : entitiesWithinAABB) { for (Entity e : entitiesWithinAABB) {
if (e.getBoundingBox().intersects(new Box(posToClean))) { if (!(e instanceof PlayerEntity) && e.getBoundingBox().intersects(new Box(posToClean))) {
e.remove(); e.remove();
} }
} }
@ -69,18 +63,43 @@ public class MyPlacementSettings extends StructurePlacementData {
return randomBlockInfos; return randomBlockInfos;
} }
private List<Structure.PalettedBlockInfoList> getEligibleStructures(List<Structure.PalettedBlockInfoList> blocks, BlockPos pos) {
List<Structure.PalettedBlockInfoList> 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) { private boolean isValid(Structure.PalettedBlockInfoList struct, BlockPos pos) {
List<Entity> entitiesWithinAABB = world.getNonSpectatingEntities(shouldReplaceEntities()?PlayerEntity.class:Entity.class, new Box(pos,pos.add(size)));
for (Structure.StructureBlockInfo bi : struct.getAll()) { for (Structure.StructureBlockInfo bi : struct.getAll()) {
BlockPos posToCheck = bi.pos.add(pos); BlockPos posToCheck = bi.pos.add(pos);
if (World.isValid(posToCheck)) { if (World.isValid(posToCheck)) {
Block blockToCheck = world.getBlockState(posToCheck).getBlock(); for (Entity e : entitiesWithinAABB) {
for (Block b : blacklist) { if (e.getBoundingBox().intersects(new Box(posToCheck))) {
if (blockToCheck.equals(b)) {
return false; return false;
} }
} }
Block blockToCheck = world.getBlockState(posToCheck).getBlock();
if(blacklist.contains(blockToCheck))
return false;
} }
} }
return true; return true;
} }
public boolean shouldReplaceEntities() {
return replaceEntities;
}
public void setReplaceEntities(boolean replaceEntities) {
this.replaceEntities = replaceEntities;
}
} }

View File

@ -1,6 +1,6 @@
{ {
"items.structure.spawner.invalid.block.clicked": "Digga %s is net %s, maan.", "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.invalid.structure.name": "Invalid structure name",
"items.structure.spawner.structure.nonexistent": "Structure: %s does not exist", "items.structure.spawner.structure.nonexistent": "Structure: %s does not exist",
"items.structure.spawner.invalid.block": "Block %s invalid", "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.xyz":" x: %s y: %s z: %s",
"item.structure_item.item.tooltip.dynamic.offset": "Dynamic offset", "item.structure_item.item.tooltip.dynamic.offset": "Dynamic offset",
"item.structure_item.item.tooltip.blacklist": "Blacklist:", "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"
} }