Merge branch '1.16.4' of https://github.com/fnuecke/oc2 into feature/crafting

This commit is contained in:
lucsoft
2021-01-10 23:24:16 +01:00
19 changed files with 184 additions and 40 deletions

View File

@@ -1,4 +1,4 @@
# OpenComputers2 [Working Title]
# OpenComputers II [Working Title]
Yet another computer mod.

View File

@@ -39,7 +39,7 @@ if (build_number == null)
version = "${semver}+${build_number}"
group = mod_group
archivesBaseName = "${mod_name}-MC${minecraft_version}-Forge"
archivesBaseName = "${mod_name.replace(' ', '-')}-MC${minecraft_version}-Forge"
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
compileJava.sourceCompatibility = compileJava.targetCompatibility = JavaVersion.VERSION_1_8

View File

@@ -12,7 +12,7 @@ forge_version_min=35
mod_group=li.cil.oc2
mod_id=oc2
mod_name=OpenComputers2
mod_name=OpenComputers II
mod_url=https://github.com/fnuecke/oc2
mod_issues_url=https://github.com/fnuecke/oc2/issues
version_major=0

View File

@@ -2,6 +2,7 @@ package li.cil.oc2.client.item;
import li.cil.oc2.api.API;
import li.cil.oc2.common.item.AbstractStorageItem;
import li.cil.oc2.common.item.HardDriveItem;
import li.cil.oc2.common.item.Items;
import net.minecraft.item.ItemModelsProperties;
import net.minecraft.util.ResourceLocation;
@@ -15,6 +16,6 @@ public final class CustomItemModelProperties {
ItemModelsProperties.registerProperty(Items.MEMORY_ITEM.get(), CustomItemModelProperties.CAPACITY_PROPERTY,
(stack, world, entity) -> AbstractStorageItem.getCapacity(stack));
ItemModelsProperties.registerProperty(Items.HARD_DRIVE_ITEM.get(), CustomItemModelProperties.CAPACITY_PROPERTY,
(stack, world, entity) -> AbstractStorageItem.getCapacity(stack));
(stack, world, entity) -> HardDriveItem.getBaseBlockDevice(stack) != null ? Integer.MAX_VALUE : AbstractStorageItem.getCapacity(stack));
}
}

View File

@@ -31,13 +31,13 @@ public final class Constants {
public static final String WRENCH_ITEM_NAME = "wrench";
public static final String BUS_INTERFACE_ITEM_NAME = "bus_interface";
public static final String NETWORK_CABLE_NAME = "network_cable";
public static final String NETWORK_CABLE_ITEM_NAME = "network_cable";
public static final String FLASH_MEMORY_ITEM_NAME = "flash_memory";
public static final String MEMORY_ITEM_NAME = "memory";
public static final String HARD_DRIVE_ITEM_NAME = "hard_drive";
public static final String REDSTONE_INTERFACE_CARD_NAME = "redstone_interface_card";
public static final String NETWORK_INTERFACE_CARD_NAME = "network_interface_card";
public static final String REDSTONE_INTERFACE_CARD_ITEM_NAME = "redstone_interface_card";
public static final String NETWORK_INTERFACE_CARD_ITEM_NAME = "network_interface_card";
public static final String CONTROL_UNIT_ITEM_NAME = "control_unit";
public static final String ARITHMETIC_LOGIC_UNIT_ITEM_NAME = "arithmetic_logic_unit";
public static final String MICROCHIP_ITEM_NAME = "microchip";

View File

@@ -6,12 +6,11 @@ import li.cil.oc2.common.util.NBTTagIds;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.NonNullList;
import net.minecraftforge.items.ItemStackHandler;
import java.util.List;
import java.util.function.Function;
public class DeviceItemStackHandler extends ItemStackHandler {
public class DeviceItemStackHandler extends FixedSizeItemStackHandler {
private static final String BUS_ELEMENT_TAG_NAME = "busElement";
///////////////////////////////////////////////////////////////////

View File

@@ -0,0 +1,38 @@
package li.cil.oc2.common.container;
import li.cil.oc2.common.util.NBTTagIds;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.NonNullList;
import net.minecraftforge.items.ItemStackHandler;
public class FixedSizeItemStackHandler extends ItemStackHandler {
private static final String SIZE_TAG_NAME = "Size";
///////////////////////////////////////////////////////////////////
public FixedSizeItemStackHandler() {
}
public FixedSizeItemStackHandler(final int size) {
super(size);
}
public FixedSizeItemStackHandler(final NonNullList<ItemStack> stacks) {
super(stacks);
}
///////////////////////////////////////////////////////////////////
@Override
public void deserializeNBT(final CompoundNBT tag) {
// Our size is fixed, don't trust NBT data we're loading.
if (tag.contains(SIZE_TAG_NAME, NBTTagIds.TAG_INT)) {
final CompoundNBT safeTag = tag.copy();
safeTag.remove(SIZE_TAG_NAME);
super.deserializeNBT(safeTag);
} else {
super.deserializeNBT(tag);
}
}
}

View File

@@ -9,6 +9,8 @@ import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import javax.annotation.Nullable;
public abstract class AbstractStorageItem extends Item {
private static final String CAPACITY_TAG_NAME = "size";
@@ -49,16 +51,27 @@ public abstract class AbstractStorageItem extends Item {
@Override
public ITextComponent getDisplayName(final ItemStack stack) {
return new StringTextComponent("")
.append(super.getDisplayName(stack))
.appendString(" (")
.append(getDisplayNameSuffix(stack))
.appendString(")");
final ITextComponent suffix = getDisplayNameSuffix(stack);
if (suffix != null) {
return new StringTextComponent("")
.append(super.getDisplayName(stack))
.appendString(" (")
.append(suffix)
.appendString(")");
} else {
return super.getDisplayName(stack);
}
}
///////////////////////////////////////////////////////////////////
@Nullable
protected ITextComponent getDisplayNameSuffix(final ItemStack stack) {
return new StringTextComponent(TextFormatUtils.formatSize(getCapacity(stack)));
final int capacity = getCapacity(stack);
if (capacity > 0) {
return new StringTextComponent(TextFormatUtils.formatSize(capacity));
} else {
return null;
}
}
}

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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;
}
};
}

View File

@@ -29,13 +29,13 @@ public final class Items {
public static final RegistryObject<Item> WRENCH_ITEM = register(Constants.WRENCH_ITEM_NAME, WrenchItem::new);
public static final RegistryObject<Item> BUS_INTERFACE_ITEM = register(Constants.BUS_INTERFACE_ITEM_NAME, BusInterfaceItem::new);
public static final RegistryObject<Item> NETWORK_CABLE_ITEM = register(Constants.NETWORK_CABLE_NAME, NetworkCableItem::new);
public static final RegistryObject<Item> NETWORK_CABLE_ITEM = register(Constants.NETWORK_CABLE_ITEM_NAME, NetworkCableItem::new);
public static final RegistryObject<Item> MEMORY_ITEM = register(Constants.MEMORY_ITEM_NAME, MemoryItem::new, new Item.Properties());
public static final RegistryObject<Item> HARD_DRIVE_ITEM = register(Constants.HARD_DRIVE_ITEM_NAME, HardDriveItem::new, new Item.Properties());
public static final RegistryObject<Item> FLASH_MEMORY_ITEM = register(Constants.FLASH_MEMORY_ITEM_NAME, FlashMemoryItem::new, new Item.Properties());
public static final RegistryObject<Item> REDSTONE_INTERFACE_CARD_ITEM = register(Constants.REDSTONE_INTERFACE_CARD_NAME);
public static final RegistryObject<Item> NETWORK_INTERFACE_CARD_ITEM = register(Constants.NETWORK_INTERFACE_CARD_NAME);
public static final RegistryObject<Item> REDSTONE_INTERFACE_CARD_ITEM = register(Constants.REDSTONE_INTERFACE_CARD_ITEM_NAME);
public static final RegistryObject<Item> NETWORK_INTERFACE_CARD_ITEM = register(Constants.NETWORK_INTERFACE_CARD_ITEM_NAME);
public static final RegistryObject<Item> CONTROL_UNIT_ITEM = register(Constants.CONTROL_UNIT_ITEM_NAME, Item::new);
public static final RegistryObject<Item> ARITHMETIC_LOGIC_UNIT_ITEM = register(Constants.ARITHMETIC_LOGIC_UNIT_ITEM_NAME, Item::new);
public static final RegistryObject<Item> MICROCHIP_ITEM = register(Constants.MICROCHIP_ITEM_NAME, Item::new);

View File

@@ -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);
}

View File

@@ -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);
}
///////////////////////////////////////////////////////////////////

View File

@@ -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,35 @@ 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 && tag.contains(BLOCK_ENTITY_INVENTORY_TAG_NAME, NBTTagIds.TAG_COMPOUND)
? tag.getCompound(BLOCK_ENTITY_INVENTORY_TAG_NAME) : null;
}
@Nullable
public static CompoundNBT getOrCreateTileEntityInventoryTag(final ItemStack stack) {
final CompoundNBT tag = getOrCreateTileEntityTag(stack);
if (tag.contains(BLOCK_ENTITY_INVENTORY_TAG_NAME, NBTTagIds.TAG_COMPOUND)) {
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);
}

View File

@@ -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();
}
}

View File

@@ -13,9 +13,6 @@ import net.minecraft.util.text.TextFormatting;
import java.util.ArrayList;
import java.util.List;
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 TooltipUtils {
private static final ThreadLocal<List<ItemStack>> ITEM_STACKS = ThreadLocal.withInitial(ArrayList::new);
private static final ThreadLocal<IntList> ITEM_STACKS_SIZES = ThreadLocal.withInitial(IntArrayList::new);
@@ -25,10 +22,8 @@ public final class TooltipUtils {
}
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 CompoundNBT itemHandlerNbt = ItemStackUtils.getTileEntityInventoryTag(stack);
if (itemHandlerNbt != null) {
final List<ItemStack> itemStacks = ITEM_STACKS.get();
itemStacks.clear();
final IntList itemStackSizes = ITEM_STACKS_SIZES.get();

View File

@@ -1,5 +1,5 @@
{
"itemGroup.oc2.common": "OpenComputers2",
"itemGroup.oc2.common": "OpenComputers II",
"block.oc2.computer": "Computer",
"block.oc2.bus_cable": "Bus Cable",

View File

@@ -1,6 +1,6 @@
{
"pack": {
"description": "OpenComputers2 Resources",
"description": "Resources",
"pack_format": 5
}
}