1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-14 20:20:30 +00:00

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.
This commit is contained in:
Jonathan Coates 2023-06-04 11:24:04 +01:00
parent b691430889
commit 6e7cbf25e8
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
17 changed files with 106 additions and 204 deletions

View File

@ -15,8 +15,8 @@ import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.platform.PlatformHelper; import dan200.computercraft.shared.platform.PlatformHelper;
import dan200.computercraft.shared.platform.RecipeIngredients; import dan200.computercraft.shared.platform.RecipeIngredients;
import dan200.computercraft.shared.platform.RegistryWrappers; import dan200.computercraft.shared.platform.RegistryWrappers;
import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory; import dan200.computercraft.shared.pocket.items.PocketComputerItem;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory; import dan200.computercraft.shared.turtle.items.TurtleItem;
import dan200.computercraft.shared.util.ColourUtils; import dan200.computercraft.shared.util.ColourUtils;
import net.minecraft.advancements.critereon.InventoryChangeTrigger; import net.minecraft.advancements.critereon.InventoryChangeTrigger;
import net.minecraft.advancements.critereon.ItemPredicate; 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.ItemLike;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Blocks;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -93,20 +94,23 @@ class RecipeProvider extends net.minecraft.data.recipes.RecipeProvider {
} }
} }
private static List<TurtleItem> turtleItems() {
return List.of(ModRegistry.Items.TURTLE_NORMAL.get(), ModRegistry.Items.TURTLE_ADVANCED.get());
}
/** /**
* Register a crafting recipe for each turtle upgrade. * Register a crafting recipe for each turtle upgrade.
* *
* @param add The callback to add recipes. * @param add The callback to add recipes.
*/ */
private void turtleUpgrades(Consumer<FinishedRecipe> add) { private void turtleUpgrades(Consumer<FinishedRecipe> add) {
for (var family : ComputerFamily.values()) { for (var turtleItem : turtleItems()) {
var base = TurtleItemFactory.create(-1, null, -1, family, null, null, 0, null); var base = turtleItem.create(-1, null, -1, null, null, 0, null);
if (base.isEmpty()) continue;
var nameId = family.name().toLowerCase(Locale.ROOT); var nameId = turtleItem.getFamily().name().toLowerCase(Locale.ROOT);
for (var upgrade : turtleUpgrades.getGeneratedUpgrades()) { 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 ShapedRecipeBuilder
.shaped(RecipeCategory.REDSTONE, result.getItem()) .shaped(RecipeCategory.REDSTONE, result.getItem())
.group(String.format("%s:turtle_%s", ComputerCraftAPI.MOD_ID, nameId)) .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<PocketComputerItem> pocketComputerItems() {
return List.of(ModRegistry.Items.POCKET_COMPUTER_NORMAL.get(), ModRegistry.Items.POCKET_COMPUTER_ADVANCED.get());
}
/** /**
* Register a crafting recipe for each pocket upgrade. * Register a crafting recipe for each pocket upgrade.
* *
* @param add The callback to add recipes. * @param add The callback to add recipes.
*/ */
private void pocketUpgrades(Consumer<FinishedRecipe> add) { private void pocketUpgrades(Consumer<FinishedRecipe> add) {
for (var family : ComputerFamily.values()) { for (var pocket : pocketComputerItems()) {
var base = PocketComputerItemFactory.create(-1, null, -1, family, null); var base = pocket.create(-1, null, -1, null);
if (base.isEmpty()) continue; if (base.isEmpty()) continue;
var nameId = family.name().toLowerCase(Locale.ROOT); var nameId = pocket.getFamily().name().toLowerCase(Locale.ROOT);
for (var upgrade : pocketUpgrades.getGeneratedUpgrades()) { for (var upgrade : pocketUpgrades.getGeneratedUpgrades()) {
var result = PocketComputerItemFactory.create(-1, null, -1, family, upgrade); var result = pocket.create(-1, null, -1, upgrade);
ShapedRecipeBuilder ShapedRecipeBuilder
.shaped(RecipeCategory.REDSTONE, result.getItem()) .shaped(RecipeCategory.REDSTONE, result.getItem())
.group(String.format("%s:pocket_%s", ComputerCraftAPI.MOD_ID, nameId)) .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<FinishedRecipe> add, String overlay, Consumer<ShapelessRecipeBuilder> build) { private void turtleOverlay(Consumer<FinishedRecipe> add, String overlay, Consumer<ShapelessRecipeBuilder> build) {
for (var family : ComputerFamily.values()) { for (var turtleItem : turtleItems()) {
var base = TurtleItemFactory.create(-1, null, -1, family, null, null, 0, null); var base = turtleItem.create(-1, null, -1, null, null, 0, null);
if (base.isEmpty()) continue;
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 group = "%s:turtle_%s_overlay".formatted(ComputerCraftAPI.MOD_ID, nameId);
var builder = ShapelessRecipeBuilder.shapeless(RecipeCategory.REDSTONE, base.getItem()) var builder = ShapelessRecipeBuilder.shapeless(RecipeCategory.REDSTONE, base.getItem())

View File

@ -5,7 +5,6 @@
package dan200.computercraft.shared.computer.inventory; package dan200.computercraft.shared.computer.inventory;
import dan200.computercraft.core.terminal.Terminal; 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.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.menu.ComputerMenu; 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.menu.ServerInputState;
import dan200.computercraft.shared.computer.terminal.NetworkedTerminal; import dan200.computercraft.shared.computer.terminal.NetworkedTerminal;
import dan200.computercraft.shared.computer.terminal.TerminalState; import dan200.computercraft.shared.computer.terminal.TerminalState;
import dan200.computercraft.shared.config.Config;
import dan200.computercraft.shared.container.SingleContainerData; import dan200.computercraft.shared.container.SingleContainerData;
import dan200.computercraft.shared.network.container.ComputerContainerData; import dan200.computercraft.shared.network.container.ComputerContainerData;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;

View File

@ -7,21 +7,25 @@ package dan200.computercraft.shared.integration;
import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.impl.PocketUpgrades; import dan200.computercraft.impl.PocketUpgrades;
import dan200.computercraft.impl.TurtleUpgrades; import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory; import dan200.computercraft.shared.pocket.items.PocketComputerItem;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory; import dan200.computercraft.shared.turtle.items.TurtleItem;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
/** /**
* Utilities for recipe mod plugins (such as JEI). * Utilities for recipe mod plugins (such as JEI).
*/ */
public final class RecipeModHelpers { public final class RecipeModHelpers {
static final List<ComputerFamily> MAIN_FAMILIES = Arrays.asList(ComputerFamily.NORMAL, ComputerFamily.ADVANCED); static final List<ComputerFamily> MAIN_FAMILIES = Arrays.asList(ComputerFamily.NORMAL, ComputerFamily.ADVANCED);
static final List<Supplier<TurtleItem>> TURTLES = List.of(ModRegistry.Items.TURTLE_NORMAL, ModRegistry.Items.TURTLE_ADVANCED);
static final List<Supplier<PocketComputerItem>> POCKET_COMPUTERS = List.of(ModRegistry.Items.POCKET_COMPUTER_NORMAL, ModRegistry.Items.POCKET_COMPUTER_ADVANCED);
private RecipeModHelpers() { private RecipeModHelpers() {
} }
@ -49,13 +53,17 @@ public final class RecipeModHelpers {
*/ */
public static List<ItemStack> getExtraStacks() { public static List<ItemStack> getExtraStacks() {
List<ItemStack> upgradeItems = new ArrayList<>(); List<ItemStack> upgradeItems = new ArrayList<>();
for (var family : MAIN_FAMILIES) { for (var turtleSupplier : TURTLES) {
var turtle = turtleSupplier.get();
for (var upgrade : TurtleUpgrades.instance().getUpgrades()) { 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()) { for (var upgrade : PocketUpgrades.instance().getUpgrades()) {
upgradeItems.add(PocketComputerItemFactory.create(-1, null, -1, family, upgrade)); upgradeItems.add(pocket.create(-1, null, -1, upgrade));
} }
} }

View File

@ -12,9 +12,7 @@ import dan200.computercraft.api.upgrades.UpgradeBase;
import dan200.computercraft.impl.PocketUpgrades; import dan200.computercraft.impl.PocketUpgrades;
import dan200.computercraft.impl.TurtleUpgrades; import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.pocket.items.PocketComputerItem; 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.TurtleItem;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import net.minecraft.core.NonNullList; import net.minecraft.core.NonNullList;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
@ -28,7 +26,8 @@ import javax.annotation.Nullable;
import java.util.*; import java.util.*;
import java.util.function.Function; 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 * Provides dynamic recipe and usage information for upgraded turtle and pocket computers. This is intended to be
@ -218,17 +217,16 @@ public class UpgradeRecipeGenerator<T> {
private static ItemStack turtleWith(ItemStack stack, @Nullable ITurtleUpgrade left, @Nullable ITurtleUpgrade right) { private static ItemStack turtleWith(ItemStack stack, @Nullable ITurtleUpgrade left, @Nullable ITurtleUpgrade right) {
var item = (TurtleItem) stack.getItem(); var item = (TurtleItem) stack.getItem();
return TurtleItemFactory.create( return item.create(
item.getComputerID(stack), item.getLabel(stack), item.getColour(stack), item.getFamily(), item.getComputerID(stack), item.getLabel(stack), item.getColour(stack),
left, right, item.getFuelLevel(stack), item.getOverlay(stack) left, right, item.getFuelLevel(stack), item.getOverlay(stack)
); );
} }
private static ItemStack pocketWith(ItemStack stack, @Nullable IPocketUpgrade back) { private static ItemStack pocketWith(ItemStack stack, @Nullable IPocketUpgrade back) {
var item = (PocketComputerItem) stack.getItem(); var item = (PocketComputerItem) stack.getItem();
return PocketComputerItemFactory.create( return item.create(
item.getComputerID(stack), item.getLabel(stack), item.getColour(stack), item.getFamily(), item.getComputerID(stack), item.getLabel(stack), item.getColour(stack), back
back
); );
} }
@ -267,20 +265,25 @@ public class UpgradeRecipeGenerator<T> {
if (recipes != null) return recipes; if (recipes != null) return recipes;
recipes = this.recipes = new ArrayList<>(4); 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( recipes.add(turtle(
ingredient, // Right upgrade, recipe on left ingredient, // Right upgrade, recipe on left
Ingredient.of(TurtleItemFactory.create(-1, null, -1, family, null, null, 0, null)), Ingredient.of(turtleItem.create(-1, null, -1, null, null, 0, null)),
TurtleItemFactory.create(-1, null, -1, family, null, turtle, 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( recipes.add(pocket(
ingredient, ingredient,
Ingredient.of(PocketComputerItemFactory.create(-1, null, -1, family, null)), Ingredient.of(pocketItem.create(-1, null, -1, null)),
PocketComputerItemFactory.create(-1, null, -1, family, pocket) pocketItem.create(-1, null, -1, pocket)
)); ));
} }
} }

View File

@ -10,7 +10,7 @@ import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.integration.RecipeModHelpers; import dan200.computercraft.shared.integration.RecipeModHelpers;
import dan200.computercraft.shared.media.items.DiskItem; import dan200.computercraft.shared.media.items.DiskItem;
import dan200.computercraft.shared.pocket.items.PocketComputerItem; 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.IModPlugin;
import mezz.jei.api.JeiPlugin; import mezz.jei.api.JeiPlugin;
import mezz.jei.api.constants.RecipeTypes; import mezz.jei.api.constants.RecipeTypes;
@ -71,7 +71,7 @@ public class JEIComputerCraft implements IModPlugin {
*/ */
private static final IIngredientSubtypeInterpreter<ItemStack> turtleSubtype = (stack, ctx) -> { private static final IIngredientSubtypeInterpreter<ItemStack> turtleSubtype = (stack, ctx) -> {
var item = stack.getItem(); 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:"); var name = new StringBuilder("turtle:");

View File

@ -12,6 +12,7 @@ import dan200.computercraft.api.media.IMedia;
import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.core.computer.ComputerSide;
import dan200.computercraft.impl.PocketUpgrades; import dan200.computercraft.impl.PocketUpgrades;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.common.IColouredItem; import dan200.computercraft.shared.common.IColouredItem;
import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerContext; import dan200.computercraft.shared.computer.core.ServerContext;
@ -45,7 +46,6 @@ import java.util.List;
public class PocketComputerItem extends Item implements IComputerItem, IMedia, IColouredItem { public class PocketComputerItem extends Item implements IComputerItem, IMedia, IColouredItem {
private static final String NBT_UPGRADE = "Upgrade"; private static final String NBT_UPGRADE = "Upgrade";
private static final String NBT_UPGRADE_INFO = "UpgradeInfo"; private static final String NBT_UPGRADE_INFO = "UpgradeInfo";
public static final String NBT_LIGHT = "Light";
public static final String NBT_ON = "On"; public static final String NBT_ON = "On";
private static final String NBT_INSTANCE = "InstanceId"; private static final String NBT_INSTANCE = "InstanceId";
@ -58,6 +58,14 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I
this.family = family; 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) { public ItemStack create(int id, @Nullable String label, int colour, @Nullable IPocketUpgrade upgrade) {
var result = new ItemStack(this); var result = new ItemStack(this);
if (id >= 0) result.getOrCreateTag().putInt(NBT_ID, id); if (id >= 0) result.getOrCreateTag().putInt(NBT_ID, id);
@ -234,7 +242,7 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I
@Override @Override
public ItemStack withFamily(ItemStack stack, ComputerFamily family) { public ItemStack withFamily(ItemStack stack, ComputerFamily family) {
return PocketComputerItemFactory.create( return create(
getComputerID(stack), getLabel(stack), getColour(stack), getComputerID(stack), getLabel(stack), getColour(stack),
family, getUpgrade(stack) family, getUpgrade(stack)
); );

View File

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

View File

@ -7,9 +7,7 @@ package dan200.computercraft.shared.pocket.recipes;
import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.impl.PocketUpgrades; import dan200.computercraft.impl.PocketUpgrades;
import dan200.computercraft.shared.ModRegistry; 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.PocketComputerItem;
import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory;
import net.minecraft.core.RegistryAccess; import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CraftingContainer; import net.minecraft.world.inventory.CraftingContainer;
@ -31,7 +29,7 @@ public final class PocketComputerUpgradeRecipe extends CustomRecipe {
@Override @Override
public ItemStack getResultItem(RegistryAccess registryAccess) { 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 @Override
@ -86,7 +84,7 @@ public final class PocketComputerUpgradeRecipe extends CustomRecipe {
var computerID = itemComputer.getComputerID(computer); var computerID = itemComputer.getComputerID(computer);
var label = itemComputer.getLabel(computer); var label = itemComputer.getLabel(computer);
var colour = itemComputer.getColour(computer); var colour = itemComputer.getColour(computer);
return PocketComputerItemFactory.create(computerID, label, colour, family, upgrade); return itemComputer.create(computerID, label, colour, upgrade);
} }
@Override @Override

View File

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

View File

@ -11,8 +11,7 @@ import dan200.computercraft.shared.computer.blocks.AbstractComputerBlockEntity;
import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.platform.RegistryEntry; import dan200.computercraft.shared.platform.RegistryEntry;
import dan200.computercraft.shared.turtle.core.TurtleBrain; import dan200.computercraft.shared.turtle.core.TurtleBrain;
import dan200.computercraft.shared.turtle.items.ITurtleItem; import dan200.computercraft.shared.turtle.items.TurtleItem;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import dan200.computercraft.shared.util.BlockEntityHelpers; import dan200.computercraft.shared.util.BlockEntityHelpers;
import dan200.computercraft.shared.util.WaterloggableHelpers; import dan200.computercraft.shared.util.WaterloggableHelpers;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -126,7 +125,7 @@ public class TurtleBlock extends AbstractComputerBlock<TurtleBlockEntity> implem
if (!world.isClientSide && tile instanceof TurtleBlockEntity turtle) { if (!world.isClientSide && tile instanceof TurtleBlockEntity turtle) {
if (entity instanceof Player player) turtle.setOwningPlayer(player.getGameProfile()); if (entity instanceof Player player) turtle.setOwningPlayer(player.getGameProfile());
if (stack.getItem() instanceof ITurtleItem item) { if (stack.getItem() instanceof TurtleItem item) {
// Set Upgrades // Set Upgrades
for (var side : TurtleSide.values()) { for (var side : TurtleSide.values()) {
turtle.getAccess().setUpgrade(side, item.getUpgrade(stack, side)); turtle.getAccess().setUpgrade(side, item.getUpgrade(stack, side));
@ -157,7 +156,14 @@ public class TurtleBlock extends AbstractComputerBlock<TurtleBlockEntity> implem
@Override @Override
protected ItemStack getItem(AbstractComputerBlockEntity tile) { 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 @Override

View File

@ -39,7 +39,7 @@ import net.minecraft.world.phys.Vec3;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collections; 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_SIZE = 16;
public static final int INVENTORY_WIDTH = 4; public static final int INVENTORY_WIDTH = 4;
public static final int INVENTORY_HEIGHT = 4; public static final int INVENTORY_HEIGHT = 4;
@ -189,39 +189,30 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba
onTileEntityChange(); onTileEntityChange();
} }
// ITurtleTile
@Override
public @Nullable ITurtleUpgrade getUpgrade(TurtleSide side) { public @Nullable ITurtleUpgrade getUpgrade(TurtleSide side) {
return brain.getUpgrade(side); return brain.getUpgrade(side);
} }
@Override
public int getColour() { public int getColour() {
return brain.getColour(); return brain.getColour();
} }
@Override
public @Nullable ResourceLocation getOverlay() { public @Nullable ResourceLocation getOverlay() {
return brain.getOverlay(); return brain.getOverlay();
} }
@Override
public ITurtleAccess getAccess() { public ITurtleAccess getAccess() {
return brain; return brain;
} }
@Override
public Vec3 getRenderOffset(float f) { public Vec3 getRenderOffset(float f) {
return brain.getRenderOffset(f); return brain.getRenderOffset(f);
} }
@Override
public float getRenderYaw(float f) { public float getRenderYaw(float f) {
return brain.getVisualYaw(f); return brain.getVisualYaw(f);
} }
@Override
public float getToolRenderAngle(TurtleSide side, float f) { public float getToolRenderAngle(TurtleSide side, float f) {
return brain.getToolRenderAngle(side, f); return brain.getToolRenderAngle(side, f);
} }

View File

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

View File

@ -9,6 +9,7 @@ import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.turtle.ITurtleUpgrade; import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.impl.TurtleUpgrades; import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.common.IColouredItem; import dan200.computercraft.shared.common.IColouredItem;
import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.items.AbstractComputerItem; import dan200.computercraft.shared.computer.items.AbstractComputerItem;
@ -24,11 +25,25 @@ import javax.annotation.Nullable;
import static dan200.computercraft.shared.turtle.core.TurtleBrain.*; 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) { public TurtleItem(TurtleBlock block, Properties settings) {
super(block, 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( public ItemStack create(
int id, @Nullable String label, int colour, int id, @Nullable String label, int colour,
@Nullable ITurtleUpgrade leftUpgrade, @Nullable ITurtleUpgrade rightUpgrade, @Nullable ITurtleUpgrade leftUpgrade, @Nullable ITurtleUpgrade rightUpgrade,
@ -99,7 +114,7 @@ public class TurtleItem extends AbstractComputerItem implements ITurtleItem {
@Override @Override
public ItemStack withFamily(ItemStack stack, ComputerFamily family) { public ItemStack withFamily(ItemStack stack, ComputerFamily family) {
return TurtleItemFactory.create( return create(
getComputerID(stack), getLabel(stack), getComputerID(stack), getLabel(stack),
getColour(stack), family, getColour(stack), family,
getUpgrade(stack, TurtleSide.LEFT), getUpgrade(stack, TurtleSide.RIGHT), 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) { public @Nullable ITurtleUpgrade getUpgrade(ItemStack stack, TurtleSide side) {
var tag = stack.getTag(); var tag = stack.getTag();
if (tag == null) return null; 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; return tag.contains(key) ? TurtleUpgrades.instance().get(tag.getString(key)) : null;
} }
@Override
public @Nullable ResourceLocation getOverlay(ItemStack stack) { public @Nullable ResourceLocation getOverlay(ItemStack stack) {
var tag = stack.getTag(); var tag = stack.getTag();
return tag != null && tag.contains(NBT_OVERLAY) ? new ResourceLocation(tag.getString(NBT_OVERLAY)) : null; return tag != null && tag.contains(NBT_OVERLAY) ? new ResourceLocation(tag.getString(NBT_OVERLAY)) : null;
} }
@Override
public int getFuelLevel(ItemStack stack) { public int getFuelLevel(ItemStack stack) {
var tag = stack.getTag(); var tag = stack.getTag();
return tag != null && tag.contains(NBT_FUEL) ? tag.getInt(NBT_FUEL) : 0; return tag != null && tag.contains(NBT_FUEL) ? tag.getInt(NBT_FUEL) : 0;

View File

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

View File

@ -9,9 +9,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.shared.ModRegistry; 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.TurtleItem;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import net.minecraft.core.NonNullList; import net.minecraft.core.NonNullList;
import net.minecraft.core.RegistryAccess; import net.minecraft.core.RegistryAccess;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
@ -22,7 +20,7 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*; 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 { public class TurtleOverlayRecipe extends ShapelessRecipe {
private final ResourceLocation overlay; private final ResourceLocation overlay;
@ -35,12 +33,11 @@ public class TurtleOverlayRecipe extends ShapelessRecipe {
} }
private static ItemStack make(ItemStack stack, ResourceLocation overlay) { private static ItemStack make(ItemStack stack, ResourceLocation overlay) {
var turtle = (ITurtleItem) stack.getItem(); var turtle = (TurtleItem) stack.getItem();
return TurtleItemFactory.create( return turtle.create(
turtle.getComputerID(stack), turtle.getComputerID(stack),
turtle.getLabel(stack), turtle.getLabel(stack),
turtle.getColour(stack), turtle.getColour(stack),
turtle.getFamily(),
turtle.getUpgrade(stack, TurtleSide.LEFT), turtle.getUpgrade(stack, TurtleSide.LEFT),
turtle.getUpgrade(stack, TurtleSide.RIGHT), turtle.getUpgrade(stack, TurtleSide.RIGHT),
turtle.getFuelLevel(stack), turtle.getFuelLevel(stack),

View File

@ -8,7 +8,7 @@ import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.items.IComputerItem; import dan200.computercraft.shared.computer.items.IComputerItem;
import dan200.computercraft.shared.computer.recipe.ComputerFamilyRecipe; 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.core.NonNullList;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -31,7 +31,7 @@ public final class TurtleRecipe extends ComputerFamilyRecipe {
var computerID = item.getComputerID(stack); var computerID = item.getComputerID(stack);
var label = item.getLabel(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<TurtleRecipe> { public static class Serializer extends ComputerFamilyRecipe.Serializer<TurtleRecipe> {

View File

@ -8,9 +8,7 @@ import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.impl.TurtleUpgrades; import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.ModRegistry; import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.turtle.items.TurtleItem;
import dan200.computercraft.shared.turtle.items.ITurtleItem;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import net.minecraft.core.RegistryAccess; import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CraftingContainer; import net.minecraft.world.inventory.CraftingContainer;
@ -32,7 +30,7 @@ public final class TurtleUpgradeRecipe extends CustomRecipe {
@Override @Override
public ItemStack getResultItem(RegistryAccess registryAccess) { 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 @Override
@ -58,7 +56,7 @@ public final class TurtleUpgradeRecipe extends CustomRecipe {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
if (item.getItem() instanceof ITurtleItem) { if (item.getItem() instanceof TurtleItem) {
// Item is a turtle // Item is a turtle
if (turtle.isEmpty()) { if (turtle.isEmpty()) {
turtle = item; turtle = item;
@ -105,8 +103,7 @@ public final class TurtleUpgradeRecipe extends CustomRecipe {
// At this point we have a turtle + 1 or 2 items // At this point we have a turtle + 1 or 2 items
// Get the turtle we already have // Get the turtle we already have
var itemTurtle = (ITurtleItem) turtle.getItem(); var itemTurtle = (TurtleItem) turtle.getItem();
var family = itemTurtle.getFamily();
var upgrades = new ITurtleUpgrade[]{ var upgrades = new ITurtleUpgrade[]{
itemTurtle.getUpgrade(turtle, TurtleSide.LEFT), itemTurtle.getUpgrade(turtle, TurtleSide.LEFT),
itemTurtle.getUpgrade(turtle, TurtleSide.RIGHT), itemTurtle.getUpgrade(turtle, TurtleSide.RIGHT),
@ -128,7 +125,7 @@ public final class TurtleUpgradeRecipe extends CustomRecipe {
var fuelLevel = itemTurtle.getFuelLevel(turtle); var fuelLevel = itemTurtle.getFuelLevel(turtle);
var colour = itemTurtle.getColour(turtle); var colour = itemTurtle.getColour(turtle);
var overlay = itemTurtle.getOverlay(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 @Override