Shuffling initialization methods around a bit.
This commit is contained in:
@@ -13,11 +13,11 @@ import net.minecraftforge.registries.ForgeRegistryEntry;
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class AbstractItemDeviceProvider extends ForgeRegistryEntry<ItemDeviceProvider> implements ItemDeviceProvider {
|
||||
private final RegistryObject<Item> item;
|
||||
private final RegistryObject<? extends Item> item;
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
protected AbstractItemDeviceProvider(final RegistryObject<Item> item) {
|
||||
protected AbstractItemDeviceProvider(final RegistryObject<? extends Item> item) {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,10 @@ public abstract class AbstractStorageItem extends ModItem {
|
||||
return stack;
|
||||
}
|
||||
|
||||
public ItemStack withCapacity(final int capacity) {
|
||||
return withCapacity(new ItemStack(this), capacity);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public AbstractStorageItem(final Properties properties, final int defaultCapacity) {
|
||||
|
||||
102
src/main/java/li/cil/oc2/common/item/BlockDeviceItem.java
Normal file
102
src/main/java/li/cil/oc2/common/item/BlockDeviceItem.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package li.cil.oc2.common.item;
|
||||
|
||||
import li.cil.oc2.api.bus.device.data.BlockDeviceData;
|
||||
import li.cil.oc2.common.bus.device.data.BlockDeviceDataRegistration;
|
||||
import li.cil.oc2.common.util.ItemStackUtils;
|
||||
import li.cil.oc2.common.util.NBTTagIds;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.ResourceLocationException;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class BlockDeviceItem extends AbstractStorageItem {
|
||||
private static final String DATA_TAG_NAME = "data";
|
||||
private static final String READONLY_TAG_NAME = "readonly";
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@Nullable
|
||||
public static BlockDeviceData getData(final ItemStack stack) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof BlockDeviceItem)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final CompoundNBT modNbt = ItemStackUtils.getModDataTag(stack);
|
||||
if (modNbt == null || !modNbt.contains(DATA_TAG_NAME, NBTTagIds.TAG_STRING)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final String registryName = modNbt.getString(DATA_TAG_NAME);
|
||||
|
||||
try {
|
||||
return BlockDeviceDataRegistration.REGISTRY.get().getValue(new ResourceLocation(registryName));
|
||||
} catch (final ResourceLocationException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemStack withData(final ItemStack stack, final BlockDeviceData data) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof BlockDeviceItem)) {
|
||||
return stack;
|
||||
}
|
||||
|
||||
final ResourceLocation key = BlockDeviceDataRegistration.REGISTRY.get().getKey(data);
|
||||
if (key == null) {
|
||||
return stack;
|
||||
}
|
||||
|
||||
ItemStackUtils.getOrCreateModDataTag(stack).putString(DATA_TAG_NAME, key.toString());
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
public static boolean isReadonly(final ItemStack stack) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof BlockDeviceItem)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final CompoundNBT modNbt = ItemStackUtils.getModDataTag(stack);
|
||||
if (modNbt == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return modNbt.getBoolean(READONLY_TAG_NAME);
|
||||
}
|
||||
|
||||
public static ItemStack withReadonly(final ItemStack stack, final boolean readonly) {
|
||||
if (!stack.isEmpty() && stack.getItem() instanceof BlockDeviceItem) {
|
||||
ItemStackUtils.getOrCreateModDataTag(stack).putBoolean(READONLY_TAG_NAME, readonly);
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
public ItemStack withData(final BlockDeviceData data) {
|
||||
return withData(new ItemStack(this), data);
|
||||
}
|
||||
|
||||
public ItemStack withReadonly(final boolean readonly) {
|
||||
return withReadonly(new ItemStack(this), readonly);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public BlockDeviceItem(final Properties properties, final int defaultCapacity) {
|
||||
super(properties.maxStackSize(1), defaultCapacity);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@Override
|
||||
protected ITextComponent getDisplayNameSuffix(final ItemStack stack) {
|
||||
final BlockDeviceData data = getData(stack);
|
||||
if (data != null) {
|
||||
return data.getDisplayName();
|
||||
} else {
|
||||
return super.getDisplayNameSuffix(stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,10 +22,6 @@ public final class FlashMemoryItem extends AbstractStorageItem {
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public static ItemStack withCapacity(final int capacity) {
|
||||
return withCapacity(new ItemStack(Items.FLASH_MEMORY.get()), capacity);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Firmware getFirmware(final ItemStack stack) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof FlashMemoryItem)) {
|
||||
@@ -61,14 +57,18 @@ public final class FlashMemoryItem extends AbstractStorageItem {
|
||||
return stack;
|
||||
}
|
||||
|
||||
public static ItemStack withFirmware(final Firmware firmware) {
|
||||
return withFirmware(new ItemStack(Items.FLASH_MEMORY.get()), firmware);
|
||||
public ItemStack withCapacity(final int capacity) {
|
||||
return withCapacity(new ItemStack(this), capacity);
|
||||
}
|
||||
|
||||
public ItemStack withFirmware(final Firmware firmware) {
|
||||
return withFirmware(new ItemStack(this), firmware);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public FlashMemoryItem(final Properties properties) {
|
||||
super(properties, DEFAULT_CAPACITY);
|
||||
super(properties.maxStackSize(1), DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -1,105 +1,9 @@
|
||||
package li.cil.oc2.common.item;
|
||||
|
||||
import li.cil.oc2.api.bus.device.data.BaseBlockDevice;
|
||||
import li.cil.oc2.common.Constants;
|
||||
import li.cil.oc2.common.bus.device.data.BaseBlockDevices;
|
||||
import li.cil.oc2.common.util.ItemStackUtils;
|
||||
import li.cil.oc2.common.util.NBTTagIds;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.ResourceLocationException;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public final class HardDriveItem extends AbstractStorageItem {
|
||||
private static final String BASE_TAG_NAME = "base";
|
||||
private static final String READONLY_TAG_NAME = "readonly";
|
||||
|
||||
private static final int DEFAULT_CAPACITY = 2 * Constants.MEGABYTE;
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public static ItemStack withCapacity(final int capacity) {
|
||||
return withCapacity(new ItemStack(Items.HARD_DRIVE.get()), capacity);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static BaseBlockDevice getBaseBlockDevice(final ItemStack stack) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof HardDriveItem)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final CompoundNBT modNbt = ItemStackUtils.getModDataTag(stack);
|
||||
if (modNbt == null || !modNbt.contains(BASE_TAG_NAME, NBTTagIds.TAG_STRING)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final String registryName = modNbt.getString(BASE_TAG_NAME);
|
||||
|
||||
try {
|
||||
return BaseBlockDevices.REGISTRY.get().getValue(new ResourceLocation(registryName));
|
||||
} catch (final ResourceLocationException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemStack withBase(final ItemStack stack, final BaseBlockDevice baseBlockDevice) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof HardDriveItem)) {
|
||||
return stack;
|
||||
}
|
||||
|
||||
final ResourceLocation key = BaseBlockDevices.REGISTRY.get().getKey(baseBlockDevice);
|
||||
if (key == null) {
|
||||
return stack;
|
||||
}
|
||||
|
||||
ItemStackUtils.getOrCreateModDataTag(stack).putString(BASE_TAG_NAME, key.toString());
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
public static ItemStack withBase(final BaseBlockDevice baseBlockDevice) {
|
||||
return withBase(new ItemStack(Items.HARD_DRIVE.get()), baseBlockDevice);
|
||||
}
|
||||
|
||||
public static boolean isReadonly(final ItemStack stack) {
|
||||
if (stack.isEmpty() || !(stack.getItem() instanceof HardDriveItem)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final CompoundNBT modNbt = ItemStackUtils.getModDataTag(stack);
|
||||
if (modNbt == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return modNbt.getBoolean(READONLY_TAG_NAME);
|
||||
}
|
||||
|
||||
public static ItemStack withReadonly(final ItemStack stack, final boolean readonly) {
|
||||
if (!stack.isEmpty() && stack.getItem() instanceof HardDriveItem) {
|
||||
ItemStackUtils.getOrCreateModDataTag(stack).putBoolean(READONLY_TAG_NAME, readonly);
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public final class HardDriveItem extends BlockDeviceItem {
|
||||
public HardDriveItem(final Properties properties) {
|
||||
super(properties, DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@Override
|
||||
protected ITextComponent getDisplayNameSuffix(final ItemStack stack) {
|
||||
final BaseBlockDevice baseBlockDevice = getBaseBlockDevice(stack);
|
||||
if (baseBlockDevice != null) {
|
||||
return baseBlockDevice.getName();
|
||||
} else {
|
||||
return super.getDisplayNameSuffix(stack);
|
||||
}
|
||||
super(properties, 2 * Constants.MEGABYTE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package li.cil.oc2.common.item;
|
||||
import li.cil.oc2.api.API;
|
||||
import li.cil.oc2.api.bus.device.DeviceTypes;
|
||||
import li.cil.oc2.common.Constants;
|
||||
import li.cil.oc2.common.bus.device.data.BaseBlockDevices;
|
||||
import li.cil.oc2.common.bus.device.data.BlockDeviceDataRegistration;
|
||||
import li.cil.oc2.common.bus.device.data.Firmwares;
|
||||
import li.cil.oc2.common.util.ItemStackUtils;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -25,17 +25,17 @@ public final class ItemGroup {
|
||||
public void fill(final NonNullList<ItemStack> items) {
|
||||
super.fill(items);
|
||||
|
||||
items.add(FlashMemoryItem.withCapacity(4 * Constants.KILOBYTE));
|
||||
items.add(FlashMemoryItem.withFirmware(Firmwares.BUILDROOT.get()));
|
||||
items.add(Items.FLASH_MEMORY.get().withCapacity(4 * Constants.KILOBYTE));
|
||||
items.add(Items.FLASH_MEMORY.get().withFirmware(Firmwares.BUILDROOT.get()));
|
||||
|
||||
items.add(MemoryItem.withCapacity(2 * Constants.MEGABYTE));
|
||||
items.add(MemoryItem.withCapacity(4 * Constants.MEGABYTE));
|
||||
items.add(MemoryItem.withCapacity(8 * Constants.MEGABYTE));
|
||||
items.add(Items.MEMORY.get().withCapacity(2 * Constants.MEGABYTE));
|
||||
items.add(Items.MEMORY.get().withCapacity(4 * Constants.MEGABYTE));
|
||||
items.add(Items.MEMORY.get().withCapacity(8 * Constants.MEGABYTE));
|
||||
|
||||
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(Items.HARD_DRIVE.get().withCapacity(2 * Constants.MEGABYTE));
|
||||
items.add(Items.HARD_DRIVE.get().withCapacity(4 * Constants.MEGABYTE));
|
||||
items.add(Items.HARD_DRIVE.get().withCapacity(8 * Constants.MEGABYTE));
|
||||
items.add(Items.HARD_DRIVE.get().withData(BlockDeviceDataRegistration.BUILDROOT.get()));
|
||||
|
||||
items.add(getPreconfiguredComputer());
|
||||
|
||||
@@ -47,15 +47,15 @@ public final class ItemGroup {
|
||||
|
||||
final CompoundNBT computerItems = ItemStackUtils.getOrCreateTileEntityInventoryTag(computer);
|
||||
computerItems.put(DeviceTypes.MEMORY.getRegistryName().toString(), makeInventoryTag(
|
||||
MemoryItem.withCapacity(8 * Constants.MEGABYTE),
|
||||
MemoryItem.withCapacity(8 * Constants.MEGABYTE),
|
||||
MemoryItem.withCapacity(8 * Constants.MEGABYTE)
|
||||
Items.MEMORY.get().withCapacity(8 * Constants.MEGABYTE),
|
||||
Items.MEMORY.get().withCapacity(8 * Constants.MEGABYTE),
|
||||
Items.MEMORY.get().withCapacity(8 * Constants.MEGABYTE)
|
||||
));
|
||||
computerItems.put(DeviceTypes.HARD_DRIVE.getRegistryName().toString(), makeInventoryTag(
|
||||
HardDriveItem.withBase(BaseBlockDevices.BUILDROOT.get())
|
||||
Items.HARD_DRIVE.get().withData(BlockDeviceDataRegistration.BUILDROOT.get())
|
||||
));
|
||||
computerItems.put(DeviceTypes.FLASH_MEMORY.getRegistryName().toString(), makeInventoryTag(
|
||||
FlashMemoryItem.withFirmware(Firmwares.BUILDROOT.get())
|
||||
Items.FLASH_MEMORY.get().withFirmware(Firmwares.BUILDROOT.get())
|
||||
));
|
||||
computerItems.put(DeviceTypes.CARD.getRegistryName().toString(), makeInventoryTag(
|
||||
new ItemStack(Items.NETWORK_INTERFACE_CARD.get())
|
||||
|
||||
@@ -32,9 +32,9 @@ public final class Items {
|
||||
public static final RegistryObject<Item> NETWORK_CABLE = register(Constants.NETWORK_CABLE_ITEM_NAME, NetworkCableItem::new);
|
||||
public static final RegistryObject<Item> ROBOT = register(Constants.ROBOT_ENTITY_NAME, RobotItem::new, commonProperties().setISTER(() -> RobotItemStackRenderer::new));
|
||||
|
||||
public static final RegistryObject<Item> MEMORY = register(Constants.MEMORY_ITEM_NAME, MemoryItem::new, new Item.Properties());
|
||||
public static final RegistryObject<Item> HARD_DRIVE = register(Constants.HARD_DRIVE_ITEM_NAME, HardDriveItem::new, new Item.Properties());
|
||||
public static final RegistryObject<Item> FLASH_MEMORY = register(Constants.FLASH_MEMORY_ITEM_NAME, FlashMemoryItem::new, new Item.Properties());
|
||||
public static final RegistryObject<MemoryItem> MEMORY = register(Constants.MEMORY_ITEM_NAME, MemoryItem::new, new Item.Properties());
|
||||
public static final RegistryObject<HardDriveItem> HARD_DRIVE = register(Constants.HARD_DRIVE_ITEM_NAME, HardDriveItem::new, new Item.Properties());
|
||||
public static final RegistryObject<FlashMemoryItem> FLASH_MEMORY = register(Constants.FLASH_MEMORY_ITEM_NAME, FlashMemoryItem::new, new Item.Properties());
|
||||
public static final RegistryObject<Item> REDSTONE_INTERFACE_CARD = register(Constants.REDSTONE_INTERFACE_CARD_ITEM_NAME);
|
||||
public static final RegistryObject<Item> NETWORK_INTERFACE_CARD = register(Constants.NETWORK_INTERFACE_CARD_ITEM_NAME);
|
||||
|
||||
|
||||
@@ -1,19 +1,12 @@
|
||||
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.get()), capacity);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public MemoryItem(final Properties properties) {
|
||||
super(properties, DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user