Fix crash with chunkloader placed by command. Closes #2349

This commit is contained in:
drcrazy 2021-02-24 09:29:15 +00:00 committed by modmuss50
parent 8398139271
commit 96383ebaaf
2 changed files with 55 additions and 29 deletions

View file

@ -77,11 +77,6 @@ public class ChunkLoaderBlockEntity extends MachineBaseBlockEntity implements IT
} }
} }
@Override
public ItemStack getToolDrop(final PlayerEntity entityPlayer) {
return TRContent.Machine.CHUNK_LOADER.getStack();
}
private void reload() { private void reload() {
unloadAll(); unloadAll();
load(); load();
@ -102,7 +97,16 @@ public class ChunkLoaderBlockEntity extends MachineBaseBlockEntity implements IT
} }
} }
private void unloadAll() {
ChunkLoaderManager manager = ChunkLoaderManager.get(world);
manager.unloadChunkLoader(world, getPos());
}
public ChunkPos getChunkPos() {
return new ChunkPos(getPos());
}
// MachineBaseBlockEntity
@Override @Override
public void onBreak(World world, PlayerEntity playerEntity, BlockPos blockPos, BlockState blockState) { public void onBreak(World world, PlayerEntity playerEntity, BlockPos blockPos, BlockState blockState) {
if (world.isClient) { if (world.isClient) {
@ -119,20 +123,13 @@ public class ChunkLoaderBlockEntity extends MachineBaseBlockEntity implements IT
reload(); reload();
} }
private void unloadAll() {
ChunkLoaderManager manager = ChunkLoaderManager.get(world);
manager.unloadChunkLoader(world, getPos());
}
public ChunkPos getChunkPos() {
return new ChunkPos(getPos());
}
@Override @Override
public NbtCompound writeNbt(NbtCompound tagCompound) { public NbtCompound writeNbt(NbtCompound tagCompound) {
super.writeNbt(tagCompound); super.writeNbt(tagCompound);
tagCompound.putInt("radius", radius); tagCompound.putInt("radius", radius);
tagCompound.putString("ownerUdid", ownerUdid); if (ownerUdid != null && !ownerUdid.isEmpty()){
tagCompound.putString("ownerUdid", ownerUdid);
}
inventory.write(tagCompound); inventory.write(tagCompound);
return tagCompound; return tagCompound;
} }
@ -148,11 +145,25 @@ public class ChunkLoaderBlockEntity extends MachineBaseBlockEntity implements IT
inventory.read(nbttagcompound); inventory.read(nbttagcompound);
} }
// IToolDrop
@Override
public ItemStack getToolDrop(final PlayerEntity entityPlayer) {
return TRContent.Machine.CHUNK_LOADER.getStack();
}
// InventoryProvider
@Override @Override
public RebornInventory<ChunkLoaderBlockEntity> getInventory() { public RebornInventory<ChunkLoaderBlockEntity> getInventory() {
return this.inventory; return this.inventory;
} }
// BuiltScreenHandlerProvider
@Override
public BuiltScreenHandler createScreenHandler(int syncID, PlayerEntity player) {
return new ScreenHandlerBuilder("chunkloader").player(player.getInventory()).inventory().hotbar().addInventory()
.blockEntity(this).sync(this::getRadius, this::setRadius).addInventory().create(this, syncID);
}
public int getRadius() { public int getRadius() {
return radius; return radius;
} }
@ -161,10 +172,5 @@ public class ChunkLoaderBlockEntity extends MachineBaseBlockEntity implements IT
this.radius = radius; this.radius = radius;
} }
@Override
public BuiltScreenHandler createScreenHandler(int syncID, PlayerEntity player) {
return new ScreenHandlerBuilder("chunkloader").player(player.getInventory()).inventory().hotbar().addInventory()
.blockEntity(this).sync(this::getRadius, this::setRadius).addInventory().create(this, syncID);
}
} }

View file

@ -27,10 +27,12 @@ package techreborn.items.tool;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.command.BlockDataObject;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext; import net.minecraft.item.ItemUsageContext;
import net.minecraft.state.property.Property; import net.minecraft.state.property.Property;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
@ -59,23 +61,32 @@ public class DebugToolItem extends Item {
return ActionResult.FAIL; return ActionResult.FAIL;
} }
sendMessage(context, new LiteralText(getRegistryName(block))); sendMessage(context, new LiteralText(getRegistryName(block)));
for (Entry<Property<?>, Comparable<?>> entry : blockState.getEntries().entrySet()) { for (Entry<Property<?>, Comparable<?>> entry : blockState.getEntries().entrySet()) {
sendMessage(context, new LiteralText(getPropertyString(entry))); sendMessage(context, new LiteralText(getPropertyString(entry)));
} }
BlockEntity blockEntity = context.getWorld().getBlockEntity(context.getBlockPos()); BlockEntity blockEntity = context.getWorld().getBlockEntity(context.getBlockPos());
if (blockEntity != null) { if (blockEntity == null) {
sendMessage(context, new LiteralText(getBlockEntityType(blockEntity))); return ActionResult.SUCCESS;
if (Energy.valid(blockEntity)) {
sendMessage(context, new LiteralText(getRCPower(blockEntity)));
}
} }
sendMessage(context, new LiteralText(getBlockEntityType(blockEntity)));
if (Energy.valid(blockEntity)) {
sendMessage(context, new LiteralText(getRCPower(blockEntity)));
}
sendMessage(context, getBlockEntityTags(blockEntity));
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} }
private void sendMessage(ItemUsageContext context, Text string) { private void sendMessage(ItemUsageContext context, Text message) {
if (!context.getWorld().isClient) { if (context.getWorld().isClient || context.getPlayer() == null) {
context.getPlayer().sendSystemMessage(string, Util.NIL_UUID); return;
} }
context.getPlayer().sendSystemMessage(message, Util.NIL_UUID);
} }
private String getPropertyString(Entry<Property<?>, Comparable<?>> entryIn) { private String getPropertyString(Entry<Property<?>, Comparable<?>> entryIn) {
@ -102,7 +113,7 @@ public class DebugToolItem extends Item {
private String getBlockEntityType(BlockEntity blockEntity) { private String getBlockEntityType(BlockEntity blockEntity) {
String s = "" + Formatting.GREEN; String s = "" + Formatting.GREEN;
s += "Tile Entity: "; s += "Block Entity: ";
s += Formatting.BLUE; s += Formatting.BLUE;
s += blockEntity.getType().toString(); s += blockEntity.getType().toString();
@ -119,4 +130,13 @@ public class DebugToolItem extends Item {
return s; return s;
} }
private Text getBlockEntityTags(BlockEntity blockEntity){
MutableText s = new LiteralText("BlockEntity Tags:").formatted(Formatting.GREEN);
BlockDataObject bdo = new BlockDataObject(blockEntity, blockEntity.getPos());
s.append(bdo.getTag().toText());
return s;
}
} }