Make computers drop even if broken in creative mode if they contain components.
This commit is contained in:
@@ -5,6 +5,7 @@ import li.cil.oc2.client.gui.TerminalScreen;
|
||||
import li.cil.oc2.common.capabilities.Capabilities;
|
||||
import li.cil.oc2.common.container.ComputerContainer;
|
||||
import li.cil.oc2.common.integration.Wrenches;
|
||||
import li.cil.oc2.common.item.Items;
|
||||
import li.cil.oc2.common.tileentity.ComputerTileEntity;
|
||||
import li.cil.oc2.common.tileentity.TileEntities;
|
||||
import li.cil.oc2.common.util.TooltipUtils;
|
||||
@@ -53,7 +54,11 @@ public final class ComputerBlock extends HorizontalBlock {
|
||||
@Override
|
||||
public void addInformation(final ItemStack stack, @Nullable final IBlockReader world, final List<ITextComponent> tooltip, final ITooltipFlag advanced) {
|
||||
super.addInformation(stack, world, tooltip, advanced);
|
||||
TooltipUtils.addInventoryInformation(stack, tooltip);
|
||||
TooltipUtils.addInventoryInformation(stack, tooltip,
|
||||
ComputerTileEntity.MEMORY_TAG_NAME,
|
||||
ComputerTileEntity.HARD_DRIVE_TAG_NAME,
|
||||
ComputerTileEntity.FLASH_MEMORY_TAG_NAME,
|
||||
ComputerTileEntity.CARD_TAG_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -135,6 +140,21 @@ public final class ComputerBlock extends HorizontalBlock {
|
||||
return ActionResultType.SUCCESS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockHarvested(final World world, final BlockPos pos, final BlockState state, final PlayerEntity player) {
|
||||
final TileEntity tileEntity = world.getTileEntity(pos);
|
||||
if (tileEntity instanceof ComputerTileEntity) {
|
||||
final ComputerTileEntity computer = (ComputerTileEntity) tileEntity;
|
||||
if (!world.isRemote() && player.isCreative() && !computer.isEmpty()) {
|
||||
final ItemStack stack = new ItemStack(Items.COMPUTER_ITEM.get());
|
||||
computer.exportToItemStack(stack);
|
||||
spawnAsEntity(world, pos, stack);
|
||||
}
|
||||
}
|
||||
|
||||
super.onBlockHarvested(world, pos, state, player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getStateForPlacement(final BlockItemUseContext context) {
|
||||
return super.getDefaultState().with(HORIZONTAL_FACING, context.getPlacementHorizontalFacing().getOpposite());
|
||||
|
||||
@@ -67,14 +67,15 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public static final String MEMORY_TAG_NAME = "memory";
|
||||
public static final String HARD_DRIVE_TAG_NAME = "hard_drive";
|
||||
public static final String FLASH_MEMORY_TAG_NAME = "flash_memory";
|
||||
public static final String CARD_TAG_NAME = "card";
|
||||
|
||||
private static final String BUS_ELEMENT_TAG_NAME = "busElement";
|
||||
private static final String TERMINAL_TAG_NAME = "terminal";
|
||||
private static final String VIRTUAL_MACHINE_TAG_NAME = "virtualMachine";
|
||||
private static final String RUNNER_TAG_NAME = "runner";
|
||||
private static final String MEMORY_TAG_NAME = "memory";
|
||||
private static final String HARD_DRIVE_TAG_NAME = "hard_drive";
|
||||
private static final String FLASH_MEMORY_TAG_NAME = "flash_memory";
|
||||
private static final String CARD_TAG_NAME = "card";
|
||||
|
||||
private static final String BUS_STATE_TAG_NAME = "busState";
|
||||
private static final String RUN_STATE_TAG_NAME = "runState";
|
||||
@@ -459,6 +460,26 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
for (int slot = 0; slot < itemHandlers.getSlots(); slot++) {
|
||||
if (!itemHandlers.getStackInSlot(slot).isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void exportToItemStack(final ItemStack stack) {
|
||||
final CompoundNBT items = new CompoundNBT();
|
||||
items.put(MEMORY_TAG_NAME, memoryItemHandler.serializeNBT());
|
||||
items.put(HARD_DRIVE_TAG_NAME, hardDriveItemHandler.serializeNBT());
|
||||
items.put(FLASH_MEMORY_TAG_NAME, flashMemoryItemHandler.serializeNBT());
|
||||
items.put(CARD_TAG_NAME, cardItemHandler.serializeNBT());
|
||||
|
||||
stack.getOrCreateChildTag(Constants.BLOCK_ENTITY_TAG_NAME_IN_ITEM)
|
||||
.put(Constants.BLOCK_ENTITY_INVENTORY_TAG_NAME, items);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,35 +21,24 @@ public final class TooltipUtils {
|
||||
private static final ThreadLocal<IntList> ITEM_STACKS_SIZES = ThreadLocal.withInitial(IntArrayList::new);
|
||||
|
||||
public static void addInventoryInformation(final ItemStack stack, final List<ITextComponent> tooltip) {
|
||||
addInventoryInformation(stack, tooltip, new String[0]);
|
||||
}
|
||||
|
||||
public static void addInventoryInformation(final ItemStack stack, final List<ITextComponent> tooltip, final String... itemHandlerTags) {
|
||||
final CompoundNBT tileEntityNbt = stack.getChildTag(BLOCK_ENTITY_TAG_NAME_IN_ITEM);
|
||||
if (tileEntityNbt != null && tileEntityNbt.contains(BLOCK_ENTITY_INVENTORY_TAG_NAME, NBTTagIds.TAG_COMPOUND)) {
|
||||
final CompoundNBT itemHandlerNbt = tileEntityNbt.getCompound(BLOCK_ENTITY_INVENTORY_TAG_NAME);
|
||||
final ListNBT itemsNbt = itemHandlerNbt.getList("Items", NBTTagIds.TAG_COMPOUND);
|
||||
|
||||
final List<ItemStack> itemStacks = ITEM_STACKS.get();
|
||||
itemStacks.clear();
|
||||
final IntList itemStackSizes = ITEM_STACKS_SIZES.get();
|
||||
itemStackSizes.clear();
|
||||
|
||||
for (int i = 0; i < itemsNbt.size(); i++) {
|
||||
final CompoundNBT itemNbt = itemsNbt.getCompound(i);
|
||||
final ItemStack itemStack = ItemStack.read(itemNbt);
|
||||
collectItemStacks(itemHandlerNbt, itemStacks, itemStackSizes);
|
||||
|
||||
boolean didMerge = false;
|
||||
for (int j = 0; j < itemStacks.size(); j++) {
|
||||
final ItemStack existingStack = itemStacks.get(j);
|
||||
if (ItemStack.areItemsEqual(existingStack, itemStack) &&
|
||||
ItemStack.areItemStackTagsEqual(existingStack, itemStack)) {
|
||||
final int existingCount = itemStackSizes.getInt(j);
|
||||
itemStackSizes.set(j, existingCount + itemStack.getCount());
|
||||
didMerge = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!didMerge) {
|
||||
itemStacks.add(itemStack);
|
||||
itemStackSizes.add(itemStack.getCount());
|
||||
for (final String itemHandlerTagName : itemHandlerTags) {
|
||||
if (itemHandlerNbt.contains(itemHandlerTagName, NBTTagIds.TAG_COMPOUND)) {
|
||||
collectItemStacks(itemHandlerNbt.getCompound(itemHandlerTagName), itemStacks, itemStackSizes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,4 +54,29 @@ public final class TooltipUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void collectItemStacks(final CompoundNBT nbt, final List<ItemStack> stacks, final IntList stackSizes) {
|
||||
final ListNBT itemsNbt = nbt.getList("Items", NBTTagIds.TAG_COMPOUND);
|
||||
for (int i = 0; i < itemsNbt.size(); i++) {
|
||||
final CompoundNBT itemNbt = itemsNbt.getCompound(i);
|
||||
final ItemStack itemStack = ItemStack.read(itemNbt);
|
||||
|
||||
boolean didMerge = false;
|
||||
for (int j = 0; j < stacks.size(); j++) {
|
||||
final ItemStack existingStack = stacks.get(j);
|
||||
if (ItemStack.areItemsEqual(existingStack, itemStack) &&
|
||||
ItemStack.areItemStackTagsEqual(existingStack, itemStack)) {
|
||||
final int existingCount = stackSizes.getInt(j);
|
||||
stackSizes.set(j, existingCount + itemStack.getCount());
|
||||
didMerge = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!didMerge) {
|
||||
stacks.add(itemStack);
|
||||
stackSizes.add(itemStack.getCount());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user