From 6e7cbf25e88cebadfcd82b361e81d4e43715aab2 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 4 Jun 2023 11:24:04 +0100 Subject: [PATCH] Clean up turtle/pocket computer item creation - Remove ITurtleItem (and ITurtleBlockEntity): this was, AFAIK, mostly a relic of the pre-1.13 code where we had multiple turtle items. I do like the theory of abstracting everything out behind an interface, but given there's only one concrete implementation, I'm not convinced it's worth it right now. - Remove TurtleItemFactory/PocketComputerItemFactory: we now prefer calling the instance .create(...) method where we have the item available (for instance upgrade recipes). In the cases we don't (creating an item the first time round), we now move the static .create(...) method to the actual item class. --- .../computercraft/data/RecipeProvider.java | 37 +++++++++------- .../inventory/AbstractComputerMenu.java | 2 +- .../shared/integration/RecipeModHelpers.java | 18 +++++--- .../integration/UpgradeRecipeGenerator.java | 33 +++++++------- .../integration/jei/JEIComputerCraft.java | 4 +- .../pocket/items/PocketComputerItem.java | 12 ++++- .../items/PocketComputerItemFactory.java | 25 ----------- .../recipes/PocketComputerUpgradeRecipe.java | 6 +-- .../turtle/blocks/ITurtleBlockEntity.java | 32 -------------- .../shared/turtle/blocks/TurtleBlock.java | 14 ++++-- .../turtle/blocks/TurtleBlockEntity.java | 11 +---- .../shared/turtle/items/ITurtleItem.java | 24 ---------- .../shared/turtle/items/TurtleItem.java | 22 +++++++--- .../turtle/items/TurtleItemFactory.java | 44 ------------------- .../turtle/recipes/TurtleOverlayRecipe.java | 9 ++-- .../shared/turtle/recipes/TurtleRecipe.java | 4 +- .../turtle/recipes/TurtleUpgradeRecipe.java | 13 +++--- 17 files changed, 106 insertions(+), 204 deletions(-) delete mode 100644 projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItemFactory.java delete mode 100644 projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/ITurtleBlockEntity.java delete mode 100644 projects/common/src/main/java/dan200/computercraft/shared/turtle/items/ITurtleItem.java delete mode 100644 projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItemFactory.java diff --git a/projects/common/src/main/java/dan200/computercraft/data/RecipeProvider.java b/projects/common/src/main/java/dan200/computercraft/data/RecipeProvider.java index dd4da45cf..46854e04d 100644 --- a/projects/common/src/main/java/dan200/computercraft/data/RecipeProvider.java +++ b/projects/common/src/main/java/dan200/computercraft/data/RecipeProvider.java @@ -15,8 +15,8 @@ import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.platform.PlatformHelper; import dan200.computercraft.shared.platform.RecipeIngredients; import dan200.computercraft.shared.platform.RegistryWrappers; -import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory; -import dan200.computercraft.shared.turtle.items.TurtleItemFactory; +import dan200.computercraft.shared.pocket.items.PocketComputerItem; +import dan200.computercraft.shared.turtle.items.TurtleItem; import dan200.computercraft.shared.util.ColourUtils; import net.minecraft.advancements.critereon.InventoryChangeTrigger; import net.minecraft.advancements.critereon.ItemPredicate; @@ -38,6 +38,7 @@ import net.minecraft.world.item.crafting.SimpleCraftingRecipeSerializer; import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.block.Blocks; +import java.util.List; import java.util.Locale; import java.util.function.Consumer; @@ -93,20 +94,23 @@ class RecipeProvider extends net.minecraft.data.recipes.RecipeProvider { } } + private static List turtleItems() { + return List.of(ModRegistry.Items.TURTLE_NORMAL.get(), ModRegistry.Items.TURTLE_ADVANCED.get()); + } + /** * Register a crafting recipe for each turtle upgrade. * * @param add The callback to add recipes. */ private void turtleUpgrades(Consumer add) { - for (var family : ComputerFamily.values()) { - var base = TurtleItemFactory.create(-1, null, -1, family, null, null, 0, null); - if (base.isEmpty()) continue; + for (var turtleItem : turtleItems()) { + var base = turtleItem.create(-1, null, -1, null, null, 0, null); - var nameId = family.name().toLowerCase(Locale.ROOT); + var nameId = turtleItem.getFamily().name().toLowerCase(Locale.ROOT); for (var upgrade : turtleUpgrades.getGeneratedUpgrades()) { - var result = TurtleItemFactory.create(-1, null, -1, family, null, upgrade, -1, null); + var result = turtleItem.create(-1, null, -1, null, upgrade, -1, null); ShapedRecipeBuilder .shaped(RecipeCategory.REDSTONE, result.getItem()) .group(String.format("%s:turtle_%s", ComputerCraftAPI.MOD_ID, nameId)) @@ -125,20 +129,24 @@ class RecipeProvider extends net.minecraft.data.recipes.RecipeProvider { } } + private static List pocketComputerItems() { + return List.of(ModRegistry.Items.POCKET_COMPUTER_NORMAL.get(), ModRegistry.Items.POCKET_COMPUTER_ADVANCED.get()); + } + /** * Register a crafting recipe for each pocket upgrade. * * @param add The callback to add recipes. */ private void pocketUpgrades(Consumer add) { - for (var family : ComputerFamily.values()) { - var base = PocketComputerItemFactory.create(-1, null, -1, family, null); + for (var pocket : pocketComputerItems()) { + var base = pocket.create(-1, null, -1, null); if (base.isEmpty()) continue; - var nameId = family.name().toLowerCase(Locale.ROOT); + var nameId = pocket.getFamily().name().toLowerCase(Locale.ROOT); for (var upgrade : pocketUpgrades.getGeneratedUpgrades()) { - var result = PocketComputerItemFactory.create(-1, null, -1, family, upgrade); + var result = pocket.create(-1, null, -1, upgrade); ShapedRecipeBuilder .shaped(RecipeCategory.REDSTONE, result.getItem()) .group(String.format("%s:pocket_%s", ComputerCraftAPI.MOD_ID, nameId)) @@ -180,11 +188,10 @@ class RecipeProvider extends net.minecraft.data.recipes.RecipeProvider { } private void turtleOverlay(Consumer add, String overlay, Consumer build) { - for (var family : ComputerFamily.values()) { - var base = TurtleItemFactory.create(-1, null, -1, family, null, null, 0, null); - if (base.isEmpty()) continue; + for (var turtleItem : turtleItems()) { + var base = turtleItem.create(-1, null, -1, null, null, 0, null); - var nameId = family.name().toLowerCase(Locale.ROOT); + var nameId = turtleItem.getFamily().name().toLowerCase(Locale.ROOT); var group = "%s:turtle_%s_overlay".formatted(ComputerCraftAPI.MOD_ID, nameId); var builder = ShapelessRecipeBuilder.shapeless(RecipeCategory.REDSTONE, base.getItem()) diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/inventory/AbstractComputerMenu.java b/projects/common/src/main/java/dan200/computercraft/shared/computer/inventory/AbstractComputerMenu.java index 6d1544dec..443383e51 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/inventory/AbstractComputerMenu.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/computer/inventory/AbstractComputerMenu.java @@ -5,7 +5,6 @@ package dan200.computercraft.shared.computer.inventory; import dan200.computercraft.core.terminal.Terminal; -import dan200.computercraft.shared.config.Config; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.computer.menu.ComputerMenu; @@ -13,6 +12,7 @@ import dan200.computercraft.shared.computer.menu.ServerInputHandler; import dan200.computercraft.shared.computer.menu.ServerInputState; import dan200.computercraft.shared.computer.terminal.NetworkedTerminal; import dan200.computercraft.shared.computer.terminal.TerminalState; +import dan200.computercraft.shared.config.Config; import dan200.computercraft.shared.container.SingleContainerData; import dan200.computercraft.shared.network.container.ComputerContainerData; import net.minecraft.world.entity.player.Player; diff --git a/projects/common/src/main/java/dan200/computercraft/shared/integration/RecipeModHelpers.java b/projects/common/src/main/java/dan200/computercraft/shared/integration/RecipeModHelpers.java index 69e443890..1e93c30a9 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/integration/RecipeModHelpers.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/integration/RecipeModHelpers.java @@ -7,21 +7,25 @@ package dan200.computercraft.shared.integration; import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.impl.PocketUpgrades; import dan200.computercraft.impl.TurtleUpgrades; +import dan200.computercraft.shared.ModRegistry; import dan200.computercraft.shared.computer.core.ComputerFamily; -import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory; -import dan200.computercraft.shared.turtle.items.TurtleItemFactory; +import dan200.computercraft.shared.pocket.items.PocketComputerItem; +import dan200.computercraft.shared.turtle.items.TurtleItem; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.Supplier; /** * Utilities for recipe mod plugins (such as JEI). */ public final class RecipeModHelpers { static final List MAIN_FAMILIES = Arrays.asList(ComputerFamily.NORMAL, ComputerFamily.ADVANCED); + static final List> TURTLES = List.of(ModRegistry.Items.TURTLE_NORMAL, ModRegistry.Items.TURTLE_ADVANCED); + static final List> POCKET_COMPUTERS = List.of(ModRegistry.Items.POCKET_COMPUTER_NORMAL, ModRegistry.Items.POCKET_COMPUTER_ADVANCED); private RecipeModHelpers() { } @@ -49,13 +53,17 @@ public final class RecipeModHelpers { */ public static List getExtraStacks() { List upgradeItems = new ArrayList<>(); - for (var family : MAIN_FAMILIES) { + for (var turtleSupplier : TURTLES) { + var turtle = turtleSupplier.get(); for (var upgrade : TurtleUpgrades.instance().getUpgrades()) { - upgradeItems.add(TurtleItemFactory.create(-1, null, -1, family, null, upgrade, 0, null)); + upgradeItems.add(turtle.create(-1, null, -1, null, upgrade, 0, null)); } + } + for (var pocketSupplier : POCKET_COMPUTERS) { + var pocket = pocketSupplier.get(); for (var upgrade : PocketUpgrades.instance().getUpgrades()) { - upgradeItems.add(PocketComputerItemFactory.create(-1, null, -1, family, upgrade)); + upgradeItems.add(pocket.create(-1, null, -1, upgrade)); } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/integration/UpgradeRecipeGenerator.java b/projects/common/src/main/java/dan200/computercraft/shared/integration/UpgradeRecipeGenerator.java index 36e86ed51..0dbe35b5d 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/integration/UpgradeRecipeGenerator.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/integration/UpgradeRecipeGenerator.java @@ -12,9 +12,7 @@ import dan200.computercraft.api.upgrades.UpgradeBase; import dan200.computercraft.impl.PocketUpgrades; import dan200.computercraft.impl.TurtleUpgrades; import dan200.computercraft.shared.pocket.items.PocketComputerItem; -import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory; import dan200.computercraft.shared.turtle.items.TurtleItem; -import dan200.computercraft.shared.turtle.items.TurtleItemFactory; import net.minecraft.core.NonNullList; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; @@ -28,7 +26,8 @@ import javax.annotation.Nullable; import java.util.*; import java.util.function.Function; -import static dan200.computercraft.shared.integration.RecipeModHelpers.MAIN_FAMILIES; +import static dan200.computercraft.shared.integration.RecipeModHelpers.POCKET_COMPUTERS; +import static dan200.computercraft.shared.integration.RecipeModHelpers.TURTLES; /** * Provides dynamic recipe and usage information for upgraded turtle and pocket computers. This is intended to be @@ -218,17 +217,16 @@ public class UpgradeRecipeGenerator { private static ItemStack turtleWith(ItemStack stack, @Nullable ITurtleUpgrade left, @Nullable ITurtleUpgrade right) { var item = (TurtleItem) stack.getItem(); - return TurtleItemFactory.create( - item.getComputerID(stack), item.getLabel(stack), item.getColour(stack), item.getFamily(), + return item.create( + item.getComputerID(stack), item.getLabel(stack), item.getColour(stack), left, right, item.getFuelLevel(stack), item.getOverlay(stack) ); } private static ItemStack pocketWith(ItemStack stack, @Nullable IPocketUpgrade back) { var item = (PocketComputerItem) stack.getItem(); - return PocketComputerItemFactory.create( - item.getComputerID(stack), item.getLabel(stack), item.getColour(stack), item.getFamily(), - back + return item.create( + item.getComputerID(stack), item.getLabel(stack), item.getColour(stack), back ); } @@ -267,20 +265,25 @@ public class UpgradeRecipeGenerator { if (recipes != null) return recipes; recipes = this.recipes = new ArrayList<>(4); - for (var family : MAIN_FAMILIES) { - if (turtle != null) { + + if (turtle != null) { + for (var turtleSupplier : TURTLES) { + var turtleItem = turtleSupplier.get(); recipes.add(turtle( ingredient, // Right upgrade, recipe on left - Ingredient.of(TurtleItemFactory.create(-1, null, -1, family, null, null, 0, null)), - TurtleItemFactory.create(-1, null, -1, family, null, turtle, 0, null) + Ingredient.of(turtleItem.create(-1, null, -1, null, null, 0, null)), + turtleItem.create(-1, null, -1, null, turtle, 0, null) )); } + } - if (pocket != null) { + if (pocket != null) { + for (var pocketSupplier : POCKET_COMPUTERS) { + var pocketItem = pocketSupplier.get(); recipes.add(pocket( ingredient, - Ingredient.of(PocketComputerItemFactory.create(-1, null, -1, family, null)), - PocketComputerItemFactory.create(-1, null, -1, family, pocket) + Ingredient.of(pocketItem.create(-1, null, -1, null)), + pocketItem.create(-1, null, -1, pocket) )); } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/integration/jei/JEIComputerCraft.java b/projects/common/src/main/java/dan200/computercraft/shared/integration/jei/JEIComputerCraft.java index c2c7f5825..b6b014697 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/integration/jei/JEIComputerCraft.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/integration/jei/JEIComputerCraft.java @@ -10,7 +10,7 @@ import dan200.computercraft.shared.ModRegistry; import dan200.computercraft.shared.integration.RecipeModHelpers; import dan200.computercraft.shared.media.items.DiskItem; import dan200.computercraft.shared.pocket.items.PocketComputerItem; -import dan200.computercraft.shared.turtle.items.ITurtleItem; +import dan200.computercraft.shared.turtle.items.TurtleItem; import mezz.jei.api.IModPlugin; import mezz.jei.api.JeiPlugin; import mezz.jei.api.constants.RecipeTypes; @@ -71,7 +71,7 @@ public class JEIComputerCraft implements IModPlugin { */ private static final IIngredientSubtypeInterpreter turtleSubtype = (stack, ctx) -> { var item = stack.getItem(); - if (!(item instanceof ITurtleItem turtle)) return IIngredientSubtypeInterpreter.NONE; + if (!(item instanceof TurtleItem turtle)) return IIngredientSubtypeInterpreter.NONE; var name = new StringBuilder("turtle:"); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java index 7d3abc57b..a3648ba0c 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java @@ -12,6 +12,7 @@ import dan200.computercraft.api.media.IMedia; import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.impl.PocketUpgrades; +import dan200.computercraft.shared.ModRegistry; import dan200.computercraft.shared.common.IColouredItem; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ServerContext; @@ -45,7 +46,6 @@ import java.util.List; public class PocketComputerItem extends Item implements IComputerItem, IMedia, IColouredItem { private static final String NBT_UPGRADE = "Upgrade"; private static final String NBT_UPGRADE_INFO = "UpgradeInfo"; - public static final String NBT_LIGHT = "Light"; public static final String NBT_ON = "On"; private static final String NBT_INSTANCE = "InstanceId"; @@ -58,6 +58,14 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I this.family = family; } + public static ItemStack create(int id, @Nullable String label, int colour, ComputerFamily family, @Nullable IPocketUpgrade upgrade) { + return switch (family) { + case NORMAL -> ModRegistry.Items.POCKET_COMPUTER_NORMAL.get().create(id, label, colour, upgrade); + case ADVANCED -> ModRegistry.Items.POCKET_COMPUTER_ADVANCED.get().create(id, label, colour, upgrade); + default -> ItemStack.EMPTY; + }; + } + public ItemStack create(int id, @Nullable String label, int colour, @Nullable IPocketUpgrade upgrade) { var result = new ItemStack(this); if (id >= 0) result.getOrCreateTag().putInt(NBT_ID, id); @@ -234,7 +242,7 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I @Override public ItemStack withFamily(ItemStack stack, ComputerFamily family) { - return PocketComputerItemFactory.create( + return create( getComputerID(stack), getLabel(stack), getColour(stack), family, getUpgrade(stack) ); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItemFactory.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItemFactory.java deleted file mode 100644 index 010ef708b..000000000 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItemFactory.java +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission. -// -// SPDX-License-Identifier: LicenseRef-CCPL - -package dan200.computercraft.shared.pocket.items; - -import dan200.computercraft.api.pocket.IPocketUpgrade; -import dan200.computercraft.shared.ModRegistry; -import dan200.computercraft.shared.computer.core.ComputerFamily; -import net.minecraft.world.item.ItemStack; - -import javax.annotation.Nullable; - -public final class PocketComputerItemFactory { - private PocketComputerItemFactory() { - } - - public static ItemStack create(int id, @Nullable String label, int colour, ComputerFamily family, @Nullable IPocketUpgrade upgrade) { - return switch (family) { - case NORMAL -> ModRegistry.Items.POCKET_COMPUTER_NORMAL.get().create(id, label, colour, upgrade); - case ADVANCED -> ModRegistry.Items.POCKET_COMPUTER_ADVANCED.get().create(id, label, colour, upgrade); - default -> ItemStack.EMPTY; - }; - } -} diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/recipes/PocketComputerUpgradeRecipe.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/recipes/PocketComputerUpgradeRecipe.java index 9ffa630b6..6c74ea7b6 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/recipes/PocketComputerUpgradeRecipe.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/recipes/PocketComputerUpgradeRecipe.java @@ -7,9 +7,7 @@ package dan200.computercraft.shared.pocket.recipes; import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.impl.PocketUpgrades; import dan200.computercraft.shared.ModRegistry; -import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.pocket.items.PocketComputerItem; -import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory; import net.minecraft.core.RegistryAccess; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.CraftingContainer; @@ -31,7 +29,7 @@ public final class PocketComputerUpgradeRecipe extends CustomRecipe { @Override public ItemStack getResultItem(RegistryAccess registryAccess) { - return PocketComputerItemFactory.create(-1, null, -1, ComputerFamily.NORMAL, null); + return ModRegistry.Items.POCKET_COMPUTER_NORMAL.get().create(-1, null, -1, null); } @Override @@ -86,7 +84,7 @@ public final class PocketComputerUpgradeRecipe extends CustomRecipe { var computerID = itemComputer.getComputerID(computer); var label = itemComputer.getLabel(computer); var colour = itemComputer.getColour(computer); - return PocketComputerItemFactory.create(computerID, label, colour, family, upgrade); + return itemComputer.create(computerID, label, colour, upgrade); } @Override diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/ITurtleBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/ITurtleBlockEntity.java deleted file mode 100644 index 2ee127bba..000000000 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/ITurtleBlockEntity.java +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission. -// -// SPDX-License-Identifier: LicenseRef-CCPL - -package dan200.computercraft.shared.turtle.blocks; - -import dan200.computercraft.api.turtle.ITurtleAccess; -import dan200.computercraft.api.turtle.ITurtleUpgrade; -import dan200.computercraft.api.turtle.TurtleSide; -import dan200.computercraft.shared.computer.blocks.IComputerBlockEntity; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.phys.Vec3; - -import javax.annotation.Nullable; - -public interface ITurtleBlockEntity extends IComputerBlockEntity { - int getColour(); - - @Nullable - ResourceLocation getOverlay(); - - @Nullable - ITurtleUpgrade getUpgrade(TurtleSide side); - - ITurtleAccess getAccess(); - - Vec3 getRenderOffset(float f); - - float getRenderYaw(float f); - - float getToolRenderAngle(TurtleSide side, float f); -} diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlock.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlock.java index 59c3f0c99..7e058698e 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlock.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlock.java @@ -11,8 +11,7 @@ import dan200.computercraft.shared.computer.blocks.AbstractComputerBlockEntity; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.platform.RegistryEntry; import dan200.computercraft.shared.turtle.core.TurtleBrain; -import dan200.computercraft.shared.turtle.items.ITurtleItem; -import dan200.computercraft.shared.turtle.items.TurtleItemFactory; +import dan200.computercraft.shared.turtle.items.TurtleItem; import dan200.computercraft.shared.util.BlockEntityHelpers; import dan200.computercraft.shared.util.WaterloggableHelpers; import net.minecraft.core.BlockPos; @@ -126,7 +125,7 @@ public class TurtleBlock extends AbstractComputerBlock implem if (!world.isClientSide && tile instanceof TurtleBlockEntity turtle) { if (entity instanceof Player player) turtle.setOwningPlayer(player.getGameProfile()); - if (stack.getItem() instanceof ITurtleItem item) { + if (stack.getItem() instanceof TurtleItem item) { // Set Upgrades for (var side : TurtleSide.values()) { turtle.getAccess().setUpgrade(side, item.getUpgrade(stack, side)); @@ -157,7 +156,14 @@ public class TurtleBlock extends AbstractComputerBlock implem @Override protected ItemStack getItem(AbstractComputerBlockEntity tile) { - return tile instanceof TurtleBlockEntity turtle ? TurtleItemFactory.create(turtle) : ItemStack.EMPTY; + if (!(tile instanceof TurtleBlockEntity turtle)) return ItemStack.EMPTY; + + var access = turtle.getAccess(); + return TurtleItem.create( + turtle.getComputerID(), turtle.getLabel(), access.getColour(), turtle.getFamily(), + access.getUpgrade(TurtleSide.LEFT), access.getUpgrade(TurtleSide.RIGHT), + access.getFuelLevel(), turtle.getOverlay() + ); } @Override diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java index 9929d4447..e5c9e2f00 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java @@ -39,7 +39,7 @@ import net.minecraft.world.phys.Vec3; import javax.annotation.Nullable; import java.util.Collections; -public class TurtleBlockEntity extends AbstractComputerBlockEntity implements BasicContainer, ITurtleBlockEntity { +public class TurtleBlockEntity extends AbstractComputerBlockEntity implements BasicContainer { public static final int INVENTORY_SIZE = 16; public static final int INVENTORY_WIDTH = 4; public static final int INVENTORY_HEIGHT = 4; @@ -189,39 +189,30 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba onTileEntityChange(); } - // ITurtleTile - - @Override public @Nullable ITurtleUpgrade getUpgrade(TurtleSide side) { return brain.getUpgrade(side); } - @Override public int getColour() { return brain.getColour(); } - @Override public @Nullable ResourceLocation getOverlay() { return brain.getOverlay(); } - @Override public ITurtleAccess getAccess() { return brain; } - @Override public Vec3 getRenderOffset(float f) { return brain.getRenderOffset(f); } - @Override public float getRenderYaw(float f) { return brain.getVisualYaw(f); } - @Override public float getToolRenderAngle(TurtleSide side, float f) { return brain.getToolRenderAngle(side, f); } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/ITurtleItem.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/ITurtleItem.java deleted file mode 100644 index 5dfb4751a..000000000 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/ITurtleItem.java +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission. -// -// SPDX-License-Identifier: LicenseRef-CCPL - -package dan200.computercraft.shared.turtle.items; - -import dan200.computercraft.api.turtle.ITurtleUpgrade; -import dan200.computercraft.api.turtle.TurtleSide; -import dan200.computercraft.shared.common.IColouredItem; -import dan200.computercraft.shared.computer.items.IComputerItem; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; - -import javax.annotation.Nullable; - -public interface ITurtleItem extends IComputerItem, IColouredItem { - @Nullable - ITurtleUpgrade getUpgrade(ItemStack stack, TurtleSide side); - - int getFuelLevel(ItemStack stack); - - @Nullable - ResourceLocation getOverlay(ItemStack stack); -} diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItem.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItem.java index 59620a755..91bc45717 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItem.java @@ -9,6 +9,7 @@ import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.turtle.ITurtleUpgrade; import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.impl.TurtleUpgrades; +import dan200.computercraft.shared.ModRegistry; import dan200.computercraft.shared.common.IColouredItem; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.items.AbstractComputerItem; @@ -24,11 +25,25 @@ import javax.annotation.Nullable; import static dan200.computercraft.shared.turtle.core.TurtleBrain.*; -public class TurtleItem extends AbstractComputerItem implements ITurtleItem { +public class TurtleItem extends AbstractComputerItem implements IColouredItem { public TurtleItem(TurtleBlock block, Properties settings) { super(block, settings); } + public static ItemStack create( + int id, @Nullable String label, int colour, ComputerFamily family, + @Nullable ITurtleUpgrade leftUpgrade, @Nullable ITurtleUpgrade rightUpgrade, + int fuelLevel, @Nullable ResourceLocation overlay + ) { + return switch (family) { + case NORMAL -> + ModRegistry.Items.TURTLE_NORMAL.get().create(id, label, colour, leftUpgrade, rightUpgrade, fuelLevel, overlay); + case ADVANCED -> + ModRegistry.Items.TURTLE_ADVANCED.get().create(id, label, colour, leftUpgrade, rightUpgrade, fuelLevel, overlay); + default -> ItemStack.EMPTY; + }; + } + public ItemStack create( int id, @Nullable String label, int colour, @Nullable ITurtleUpgrade leftUpgrade, @Nullable ITurtleUpgrade rightUpgrade, @@ -99,7 +114,7 @@ public class TurtleItem extends AbstractComputerItem implements ITurtleItem { @Override public ItemStack withFamily(ItemStack stack, ComputerFamily family) { - return TurtleItemFactory.create( + return create( getComputerID(stack), getLabel(stack), getColour(stack), family, getUpgrade(stack, TurtleSide.LEFT), getUpgrade(stack, TurtleSide.RIGHT), @@ -107,7 +122,6 @@ public class TurtleItem extends AbstractComputerItem implements ITurtleItem { ); } - @Override public @Nullable ITurtleUpgrade getUpgrade(ItemStack stack, TurtleSide side) { var tag = stack.getTag(); if (tag == null) return null; @@ -116,13 +130,11 @@ public class TurtleItem extends AbstractComputerItem implements ITurtleItem { return tag.contains(key) ? TurtleUpgrades.instance().get(tag.getString(key)) : null; } - @Override public @Nullable ResourceLocation getOverlay(ItemStack stack) { var tag = stack.getTag(); return tag != null && tag.contains(NBT_OVERLAY) ? new ResourceLocation(tag.getString(NBT_OVERLAY)) : null; } - @Override public int getFuelLevel(ItemStack stack) { var tag = stack.getTag(); return tag != null && tag.contains(NBT_FUEL) ? tag.getInt(NBT_FUEL) : 0; diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItemFactory.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItemFactory.java deleted file mode 100644 index 81953a899..000000000 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItemFactory.java +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission. -// -// SPDX-License-Identifier: LicenseRef-CCPL - -package dan200.computercraft.shared.turtle.items; - -import dan200.computercraft.api.turtle.ITurtleUpgrade; -import dan200.computercraft.api.turtle.TurtleSide; -import dan200.computercraft.shared.ModRegistry; -import dan200.computercraft.shared.computer.core.ComputerFamily; -import dan200.computercraft.shared.turtle.blocks.ITurtleBlockEntity; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; - -import javax.annotation.Nullable; - -public final class TurtleItemFactory { - private TurtleItemFactory() { - } - - public static ItemStack create(ITurtleBlockEntity turtle) { - var access = turtle.getAccess(); - - return create( - turtle.getComputerID(), turtle.getLabel(), turtle.getColour(), turtle.getFamily(), - access.getUpgrade(TurtleSide.LEFT), access.getUpgrade(TurtleSide.RIGHT), - access.getFuelLevel(), turtle.getOverlay() - ); - } - - public static ItemStack create( - int id, @Nullable String label, int colour, ComputerFamily family, - @Nullable ITurtleUpgrade leftUpgrade, @Nullable ITurtleUpgrade rightUpgrade, - int fuelLevel, @Nullable ResourceLocation overlay - ) { - return switch (family) { - case NORMAL -> - ModRegistry.Items.TURTLE_NORMAL.get().create(id, label, colour, leftUpgrade, rightUpgrade, fuelLevel, overlay); - case ADVANCED -> - ModRegistry.Items.TURTLE_ADVANCED.get().create(id, label, colour, leftUpgrade, rightUpgrade, fuelLevel, overlay); - default -> ItemStack.EMPTY; - }; - } -} diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleOverlayRecipe.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleOverlayRecipe.java index 4e6cf5c0b..4076a3662 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleOverlayRecipe.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleOverlayRecipe.java @@ -9,9 +9,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.shared.ModRegistry; -import dan200.computercraft.shared.turtle.items.ITurtleItem; import dan200.computercraft.shared.turtle.items.TurtleItem; -import dan200.computercraft.shared.turtle.items.TurtleItemFactory; import net.minecraft.core.NonNullList; import net.minecraft.core.RegistryAccess; import net.minecraft.network.FriendlyByteBuf; @@ -22,7 +20,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.*; /** - * A {@link ShapelessRecipe} which sets the {@linkplain ITurtleItem#getOverlay(ItemStack)} turtle's overlay} instead. + * A {@link ShapelessRecipe} which sets the {@linkplain TurtleItem#getOverlay(ItemStack)} turtle's overlay} instead. */ public class TurtleOverlayRecipe extends ShapelessRecipe { private final ResourceLocation overlay; @@ -35,12 +33,11 @@ public class TurtleOverlayRecipe extends ShapelessRecipe { } private static ItemStack make(ItemStack stack, ResourceLocation overlay) { - var turtle = (ITurtleItem) stack.getItem(); - return TurtleItemFactory.create( + var turtle = (TurtleItem) stack.getItem(); + return turtle.create( turtle.getComputerID(stack), turtle.getLabel(stack), turtle.getColour(stack), - turtle.getFamily(), turtle.getUpgrade(stack, TurtleSide.LEFT), turtle.getUpgrade(stack, TurtleSide.RIGHT), turtle.getFuelLevel(stack), diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleRecipe.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleRecipe.java index 2451c8324..2b786a5db 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleRecipe.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleRecipe.java @@ -8,7 +8,7 @@ import dan200.computercraft.shared.ModRegistry; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.items.IComputerItem; import dan200.computercraft.shared.computer.recipe.ComputerFamilyRecipe; -import dan200.computercraft.shared.turtle.items.TurtleItemFactory; +import dan200.computercraft.shared.turtle.items.TurtleItem; import net.minecraft.core.NonNullList; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; @@ -31,7 +31,7 @@ public final class TurtleRecipe extends ComputerFamilyRecipe { var computerID = item.getComputerID(stack); var label = item.getLabel(stack); - return TurtleItemFactory.create(computerID, label, -1, getFamily(), null, null, 0, null); + return TurtleItem.create(computerID, label, -1, getFamily(), null, null, 0, null); } public static class Serializer extends ComputerFamilyRecipe.Serializer { diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleUpgradeRecipe.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleUpgradeRecipe.java index 690116d56..4dcd9e5a2 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleUpgradeRecipe.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleUpgradeRecipe.java @@ -8,9 +8,7 @@ import dan200.computercraft.api.turtle.ITurtleUpgrade; import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.impl.TurtleUpgrades; import dan200.computercraft.shared.ModRegistry; -import dan200.computercraft.shared.computer.core.ComputerFamily; -import dan200.computercraft.shared.turtle.items.ITurtleItem; -import dan200.computercraft.shared.turtle.items.TurtleItemFactory; +import dan200.computercraft.shared.turtle.items.TurtleItem; import net.minecraft.core.RegistryAccess; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.CraftingContainer; @@ -32,7 +30,7 @@ public final class TurtleUpgradeRecipe extends CustomRecipe { @Override public ItemStack getResultItem(RegistryAccess registryAccess) { - return TurtleItemFactory.create(-1, null, -1, ComputerFamily.NORMAL, null, null, 0, null); + return ModRegistry.Items.TURTLE_NORMAL.get().create(-1, null, -1, null, null, 0, null); } @Override @@ -58,7 +56,7 @@ public final class TurtleUpgradeRecipe extends CustomRecipe { return ItemStack.EMPTY; } - if (item.getItem() instanceof ITurtleItem) { + if (item.getItem() instanceof TurtleItem) { // Item is a turtle if (turtle.isEmpty()) { turtle = item; @@ -105,8 +103,7 @@ public final class TurtleUpgradeRecipe extends CustomRecipe { // At this point we have a turtle + 1 or 2 items // Get the turtle we already have - var itemTurtle = (ITurtleItem) turtle.getItem(); - var family = itemTurtle.getFamily(); + var itemTurtle = (TurtleItem) turtle.getItem(); var upgrades = new ITurtleUpgrade[]{ itemTurtle.getUpgrade(turtle, TurtleSide.LEFT), itemTurtle.getUpgrade(turtle, TurtleSide.RIGHT), @@ -128,7 +125,7 @@ public final class TurtleUpgradeRecipe extends CustomRecipe { var fuelLevel = itemTurtle.getFuelLevel(turtle); var colour = itemTurtle.getColour(turtle); var overlay = itemTurtle.getOverlay(turtle); - return TurtleItemFactory.create(computerID, label, colour, family, upgrades[0], upgrades[1], fuelLevel, overlay); + return itemTurtle.create(computerID, label, colour, upgrades[0], upgrades[1], fuelLevel, overlay); } @Override