Add preconfigured computer to creative tab and add utility methods to make creating preconfigured items easier to do and read.
This commit is contained in:
@@ -22,6 +22,10 @@ public final class FlashMemoryItem extends AbstractStorageItem {
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public static ItemStack withCapacity(final int capacity) {
|
||||
return withCapacity(new ItemStack(Items.FLASH_MEMORY_ITEM.get()), capacity);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Firmware getFirmware(final ItemStack stack) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof FlashMemoryItem)) {
|
||||
@@ -57,6 +61,10 @@ public final class FlashMemoryItem extends AbstractStorageItem {
|
||||
return stack;
|
||||
}
|
||||
|
||||
public static ItemStack withFirmware(final Firmware firmware) {
|
||||
return withFirmware(new ItemStack(Items.FLASH_MEMORY_ITEM.get()), firmware);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public FlashMemoryItem(final Properties properties) {
|
||||
|
||||
@@ -21,6 +21,10 @@ public final class HardDriveItem extends AbstractStorageItem {
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public static ItemStack withCapacity(final int capacity) {
|
||||
return withCapacity(new ItemStack(Items.HARD_DRIVE_ITEM.get()), capacity);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static BaseBlockDevice getBaseBlockDevice(final ItemStack stack) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof HardDriveItem)) {
|
||||
@@ -56,6 +60,10 @@ public final class HardDriveItem extends AbstractStorageItem {
|
||||
return stack;
|
||||
}
|
||||
|
||||
public static ItemStack withBase(final BaseBlockDevice baseBlockDevice) {
|
||||
return withBase(new ItemStack(Items.HARD_DRIVE_ITEM.get()), baseBlockDevice);
|
||||
}
|
||||
|
||||
public static boolean isReadonly(final ItemStack stack) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof HardDriveItem)) {
|
||||
return false;
|
||||
|
||||
@@ -4,9 +4,16 @@ import li.cil.oc2.api.API;
|
||||
import li.cil.oc2.common.Constants;
|
||||
import li.cil.oc2.common.bus.device.data.BaseBlockDevices;
|
||||
import li.cil.oc2.common.bus.device.data.Firmwares;
|
||||
import li.cil.oc2.common.tileentity.ComputerTileEntity;
|
||||
import li.cil.oc2.common.util.ItemStackUtils;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.util.NonNullList;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import static li.cil.oc2.common.util.NBTUtils.makeInventoryTag;
|
||||
|
||||
public final class ItemGroup {
|
||||
public static final net.minecraft.item.ItemGroup COMMON = new net.minecraft.item.ItemGroup(API.MOD_ID + ".common") {
|
||||
@Override
|
||||
@@ -18,17 +25,43 @@ public final class ItemGroup {
|
||||
public void fill(final NonNullList<ItemStack> items) {
|
||||
super.fill(items);
|
||||
|
||||
items.add(FlashMemoryItem.withCapacity(new ItemStack(Items.FLASH_MEMORY_ITEM.get()), 4 * Constants.KILOBYTE));
|
||||
items.add(FlashMemoryItem.withFirmware(new ItemStack(Items.FLASH_MEMORY_ITEM.get()), Firmwares.BUILDROOT.get()));
|
||||
items.add(FlashMemoryItem.withCapacity(4 * Constants.KILOBYTE));
|
||||
items.add(FlashMemoryItem.withFirmware(Firmwares.BUILDROOT.get()));
|
||||
|
||||
items.add(MemoryItem.withCapacity(new ItemStack(Items.MEMORY_ITEM.get()), 2 * Constants.MEGABYTE));
|
||||
items.add(MemoryItem.withCapacity(new ItemStack(Items.MEMORY_ITEM.get()), 4 * Constants.MEGABYTE));
|
||||
items.add(MemoryItem.withCapacity(new ItemStack(Items.MEMORY_ITEM.get()), 8 * Constants.MEGABYTE));
|
||||
items.add(MemoryItem.withCapacity(2 * Constants.MEGABYTE));
|
||||
items.add(MemoryItem.withCapacity(4 * Constants.MEGABYTE));
|
||||
items.add(MemoryItem.withCapacity(8 * Constants.MEGABYTE));
|
||||
|
||||
items.add(HardDriveItem.withCapacity(new ItemStack(Items.HARD_DRIVE_ITEM.get()), 2 * Constants.MEGABYTE));
|
||||
items.add(HardDriveItem.withCapacity(new ItemStack(Items.HARD_DRIVE_ITEM.get()), 4 * Constants.MEGABYTE));
|
||||
items.add(HardDriveItem.withCapacity(new ItemStack(Items.HARD_DRIVE_ITEM.get()), 8 * Constants.MEGABYTE));
|
||||
items.add(HardDriveItem.withBase(new ItemStack(Items.HARD_DRIVE_ITEM.get()), BaseBlockDevices.BUILDROOT.get()));
|
||||
items.add(HardDriveItem.withCapacity(2 * Constants.MEGABYTE));
|
||||
items.add(HardDriveItem.withCapacity(4 * Constants.MEGABYTE));
|
||||
items.add(HardDriveItem.withCapacity(8 * Constants.MEGABYTE));
|
||||
items.add(HardDriveItem.withBase(BaseBlockDevices.BUILDROOT.get()));
|
||||
|
||||
items.add(getPreconfiguredComputer());
|
||||
|
||||
items.sort(Comparator.comparing(ItemStack::getTranslationKey));
|
||||
}
|
||||
|
||||
private ItemStack getPreconfiguredComputer() {
|
||||
final ItemStack computer = new ItemStack(Items.COMPUTER_ITEM.get());
|
||||
|
||||
final CompoundNBT computerItems = ItemStackUtils.getOrCreateTileEntityInventoryTag(computer);
|
||||
computerItems.put(ComputerTileEntity.MEMORY_TAG_NAME, makeInventoryTag(
|
||||
MemoryItem.withCapacity(8 * Constants.MEGABYTE),
|
||||
MemoryItem.withCapacity(8 * Constants.MEGABYTE),
|
||||
MemoryItem.withCapacity(8 * Constants.MEGABYTE)
|
||||
));
|
||||
computerItems.put(ComputerTileEntity.HARD_DRIVE_TAG_NAME, makeInventoryTag(
|
||||
HardDriveItem.withBase(BaseBlockDevices.BUILDROOT.get())
|
||||
));
|
||||
computerItems.put(ComputerTileEntity.FLASH_MEMORY_TAG_NAME, makeInventoryTag(
|
||||
FlashMemoryItem.withFirmware(Firmwares.BUILDROOT.get())
|
||||
));
|
||||
computerItems.put(ComputerTileEntity.CARD_TAG_NAME, makeInventoryTag(
|
||||
new ItemStack(Items.NETWORK_INTERFACE_CARD_ITEM.get())
|
||||
));
|
||||
|
||||
return computer;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
package li.cil.oc2.common.item;
|
||||
|
||||
import li.cil.oc2.common.Constants;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public final class MemoryItem extends AbstractStorageItem {
|
||||
private static final int DEFAULT_CAPACITY = 2 * Constants.MEGABYTE;
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public static ItemStack withCapacity(final int capacity) {
|
||||
return withCapacity(new ItemStack(Items.MEMORY_ITEM.get()), capacity);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public MemoryItem(final Properties properties) {
|
||||
super(properties, DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import li.cil.oc2.common.network.message.ComputerRunStateMessage;
|
||||
import li.cil.oc2.common.network.message.TerminalBlockOutputMessage;
|
||||
import li.cil.oc2.common.serialization.NBTSerialization;
|
||||
import li.cil.oc2.common.util.HorizontalBlockUtils;
|
||||
import li.cil.oc2.common.util.ItemStackUtils;
|
||||
import li.cil.oc2.common.util.NBTTagIds;
|
||||
import li.cil.oc2.common.util.NBTUtils;
|
||||
import li.cil.oc2.common.vm.Terminal;
|
||||
@@ -500,14 +501,11 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic
|
||||
}
|
||||
|
||||
public void exportToItemStack(final ItemStack stack) {
|
||||
final CompoundNBT items = new CompoundNBT();
|
||||
final CompoundNBT items = ItemStackUtils.getOrCreateTileEntityInventoryTag(stack);
|
||||
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);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -13,6 +13,9 @@ import javax.annotation.Nullable;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
import static li.cil.oc2.common.Constants.BLOCK_ENTITY_INVENTORY_TAG_NAME;
|
||||
import static li.cil.oc2.common.Constants.BLOCK_ENTITY_TAG_NAME_IN_ITEM;
|
||||
|
||||
public final class ItemStackUtils {
|
||||
private static final String MOD_TAG_NAME = API.MOD_ID;
|
||||
|
||||
@@ -33,6 +36,34 @@ public final class ItemStackUtils {
|
||||
return stack.getOrCreateChildTag(MOD_TAG_NAME);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CompoundNBT getTileEntityTag(final ItemStack stack) {
|
||||
return stack.getChildTag(BLOCK_ENTITY_TAG_NAME_IN_ITEM);
|
||||
}
|
||||
|
||||
public static CompoundNBT getOrCreateTileEntityTag(final ItemStack stack) {
|
||||
return stack.getOrCreateChildTag(BLOCK_ENTITY_TAG_NAME_IN_ITEM);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CompoundNBT getTileEntityInventoryTag(final ItemStack stack) {
|
||||
final CompoundNBT tag = getTileEntityTag(stack);
|
||||
return tag == null ? null : tag.getCompound(BLOCK_ENTITY_INVENTORY_TAG_NAME);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CompoundNBT getOrCreateTileEntityInventoryTag(final ItemStack stack) {
|
||||
final CompoundNBT tag = getOrCreateTileEntityTag(stack);
|
||||
if (tag.contains(BLOCK_ENTITY_INVENTORY_TAG_NAME)) {
|
||||
return tag.getCompound(BLOCK_ENTITY_INVENTORY_TAG_NAME);
|
||||
}
|
||||
|
||||
final CompoundNBT inventoryNbt = new CompoundNBT();
|
||||
tag.put(BLOCK_ENTITY_INVENTORY_TAG_NAME, inventoryNbt);
|
||||
|
||||
return inventoryNbt;
|
||||
}
|
||||
|
||||
public static Optional<ItemEntity> spawnAsEntity(final World world, final BlockPos pos, final ItemStack stack) {
|
||||
return spawnAsEntity(world, Vector3d.copyCentered(pos), stack);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package li.cil.oc2.common.util;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -24,4 +26,12 @@ public final class NBTUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static CompoundNBT makeInventoryTag(final ItemStack... items) {
|
||||
final ItemStackHandler itemStackHandler = new ItemStackHandler(items.length);
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
itemStackHandler.setStackInSlot(i, items[i]);
|
||||
}
|
||||
return itemStackHandler.serializeNBT();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user