Fix crash with chunkloader placed by command. Closes #2349

This commit is contained in:
drcrazy 2021-02-24 12:29:15 +03:00
parent 6877f6e3f2
commit 9b47348fd3
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() {
unloadAll();
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
public void onBreak(World world, PlayerEntity playerEntity, BlockPos blockPos, BlockState blockState) {
if (world.isClient) {
@ -119,20 +123,13 @@ public class ChunkLoaderBlockEntity extends MachineBaseBlockEntity implements IT
reload();
}
private void unloadAll() {
ChunkLoaderManager manager = ChunkLoaderManager.get(world);
manager.unloadChunkLoader(world, getPos());
}
public ChunkPos getChunkPos() {
return new ChunkPos(getPos());
}
@Override
public CompoundTag toTag(CompoundTag tagCompound) {
super.toTag(tagCompound);
tagCompound.putInt("radius", radius);
tagCompound.putString("ownerUdid", ownerUdid);
if (ownerUdid != null && !ownerUdid.isEmpty()){
tagCompound.putString("ownerUdid", ownerUdid);
}
inventory.write(tagCompound);
return tagCompound;
}
@ -148,11 +145,25 @@ public class ChunkLoaderBlockEntity extends MachineBaseBlockEntity implements IT
inventory.read(nbttagcompound);
}
// IToolDrop
@Override
public ItemStack getToolDrop(final PlayerEntity entityPlayer) {
return TRContent.Machine.CHUNK_LOADER.getStack();
}
// InventoryProvider
@Override
public RebornInventory<ChunkLoaderBlockEntity> getInventory() {
return this.inventory;
}
// BuiltScreenHandlerProvider
@Override
public BuiltScreenHandler createScreenHandler(int syncID, PlayerEntity player) {
return new ScreenHandlerBuilder("chunkloader").player(player.inventory).inventory().hotbar().addInventory()
.blockEntity(this).sync(this::getRadius, this::setRadius).addInventory().create(this, syncID);
}
public int getRadius() {
return radius;
}
@ -161,10 +172,5 @@ public class ChunkLoaderBlockEntity extends MachineBaseBlockEntity implements IT
this.radius = radius;
}
@Override
public BuiltScreenHandler createScreenHandler(int syncID, PlayerEntity player) {
return new ScreenHandlerBuilder("chunkloader").player(player.inventory).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.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.command.BlockDataObject;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.state.property.Property;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
@ -59,23 +61,32 @@ public class DebugToolItem extends Item {
return ActionResult.FAIL;
}
sendMessage(context, new LiteralText(getRegistryName(block)));
for (Entry<Property<?>, Comparable<?>> entry : blockState.getEntries().entrySet()) {
sendMessage(context, new LiteralText(getPropertyString(entry)));
}
BlockEntity blockEntity = context.getWorld().getBlockEntity(context.getBlockPos());
if (blockEntity != null) {
sendMessage(context, new LiteralText(getBlockEntityType(blockEntity)));
if (Energy.valid(blockEntity)) {
sendMessage(context, new LiteralText(getRCPower(blockEntity)));
}
if (blockEntity == null) {
return ActionResult.SUCCESS;
}
sendMessage(context, new LiteralText(getBlockEntityType(blockEntity)));
if (Energy.valid(blockEntity)) {
sendMessage(context, new LiteralText(getRCPower(blockEntity)));
}
sendMessage(context, getBlockEntityTags(blockEntity));
return ActionResult.SUCCESS;
}
private void sendMessage(ItemUsageContext context, Text string) {
if (!context.getWorld().isClient) {
context.getPlayer().sendSystemMessage(string, Util.NIL_UUID);
private void sendMessage(ItemUsageContext context, Text message) {
if (context.getWorld().isClient || context.getPlayer() == null) {
return;
}
context.getPlayer().sendSystemMessage(message, Util.NIL_UUID);
}
private String getPropertyString(Entry<Property<?>, Comparable<?>> entryIn) {
@ -102,7 +113,7 @@ public class DebugToolItem extends Item {
private String getBlockEntityType(BlockEntity blockEntity) {
String s = "" + Formatting.GREEN;
s += "Tile Entity: ";
s += "Block Entity: ";
s += Formatting.BLUE;
s += blockEntity.getType().toString();
@ -119,4 +130,13 @@ public class DebugToolItem extends Item {
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;
}
}