diff --git a/projects/common-api/src/client/java/dan200/computercraft/api/client/turtle/TurtleUpgradeModeller.java b/projects/common-api/src/client/java/dan200/computercraft/api/client/turtle/TurtleUpgradeModeller.java index 1e52e7971..f927547b0 100644 --- a/projects/common-api/src/client/java/dan200/computercraft/api/client/turtle/TurtleUpgradeModeller.java +++ b/projects/common-api/src/client/java/dan200/computercraft/api/client/turtle/TurtleUpgradeModeller.java @@ -13,8 +13,7 @@ import net.minecraft.core.component.DataComponentPatch; import net.minecraft.resources.ResourceLocation; import javax.annotation.Nullable; -import java.util.Collection; -import java.util.List; +import java.util.stream.Stream; /** * Provides models for a {@link ITurtleUpgrade}. @@ -41,17 +40,17 @@ public interface TurtleUpgradeModeller { TransformedModel getModel(T upgrade, @Nullable ITurtleAccess turtle, TurtleSide side, DataComponentPatch data); /** - * Get a list of models that this turtle modeller depends on. + * Get the models that this turtle modeller depends on. *

- * Models included in this list will be loaded and baked alongside item and block models, and so may be referenced + * Models included in this stream will be loaded and baked alongside item and block models, and so may be referenced * by {@link TransformedModel#of(ResourceLocation)}. You do not need to override this method if you will load models * by other means. * * @return A list of models that this modeller depends on. * @see UnbakedModel#getDependencies() */ - default Collection getDependencies() { - return List.of(); + default Stream getDependencies() { + return Stream.of(); } /** @@ -85,8 +84,8 @@ public interface TurtleUpgradeModeller { } @Override - public Collection getDependencies() { - return List.of(left, right); + public Stream getDependencies() { + return Stream.of(left, right); } }; } diff --git a/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java b/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java index d75ae2986..3aeb87644 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java +++ b/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java @@ -125,8 +125,7 @@ public final class ClientRegistry { new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_crafting_table_left"), new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_crafting_table_right") )); - register.register(ModRegistry.TurtleUpgradeTypes.WIRELESS_MODEM_NORMAL.get(), new TurtleModemModeller(false)); - register.register(ModRegistry.TurtleUpgradeTypes.WIRELESS_MODEM_ADVANCED.get(), new TurtleModemModeller(true)); + register.register(ModRegistry.TurtleUpgradeTypes.WIRELESS_MODEM.get(), new TurtleModemModeller()); register.register(ModRegistry.TurtleUpgradeTypes.TOOL.get(), TurtleUpgradeModeller.flatItem()); } diff --git a/projects/common/src/client/java/dan200/computercraft/client/turtle/TurtleModemModeller.java b/projects/common/src/client/java/dan200/computercraft/client/turtle/TurtleModemModeller.java index bc5302685..cf775240c 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/turtle/TurtleModemModeller.java +++ b/projects/common/src/client/java/dan200/computercraft/client/turtle/TurtleModemModeller.java @@ -15,44 +15,46 @@ import net.minecraft.core.component.DataComponentPatch; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.Nullable; -import java.util.Collection; -import java.util.List; +import java.util.stream.Stream; /** * A {@link TurtleUpgradeModeller} for modems, providing different models depending on if the modem is on/off. */ public class TurtleModemModeller implements TurtleUpgradeModeller { - private final ResourceLocation leftOffModel; - private final ResourceLocation rightOffModel; - private final ResourceLocation leftOnModel; - private final ResourceLocation rightOnModel; - - public TurtleModemModeller(boolean advanced) { - if (advanced) { - leftOffModel = new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_advanced_off_left"); - rightOffModel = new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_advanced_off_right"); - leftOnModel = new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_advanced_on_left"); - rightOnModel = new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_advanced_on_right"); - } else { - leftOffModel = new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_normal_off_left"); - rightOffModel = new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_normal_off_right"); - leftOnModel = new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_normal_on_left"); - rightOnModel = new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_normal_on_right"); - } - } - @Override public TransformedModel getModel(TurtleModem upgrade, @Nullable ITurtleAccess turtle, TurtleSide side, DataComponentPatch data) { var component = data.get(ModRegistry.DataComponents.ON.get()); var active = component != null && component.isPresent() && component.get(); + var models = upgrade.advanced() ? ModemModels.ADVANCED : ModemModels.NORMAL; return side == TurtleSide.LEFT - ? TransformedModel.of(active ? leftOnModel : leftOffModel) - : TransformedModel.of(active ? rightOnModel : rightOffModel); + ? TransformedModel.of(active ? models.leftOnModel() : models.leftOffModel()) + : TransformedModel.of(active ? models.rightOnModel() : models.rightOffModel()); } @Override - public Collection getDependencies() { - return List.of(leftOffModel, rightOffModel, leftOnModel, rightOnModel); + public Stream getDependencies() { + return Stream.of(ModemModels.NORMAL, ModemModels.ADVANCED).flatMap(ModemModels::getDependencies); + } + + private record ModemModels( + ResourceLocation leftOffModel, ResourceLocation rightOffModel, + ResourceLocation leftOnModel, ResourceLocation rightOnModel + ) { + private static final ModemModels NORMAL = create("normal"); + private static final ModemModels ADVANCED = create("advanced"); + + public static ModemModels create(String type) { + return new ModemModels( + new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_" + type + "_off_left"), + new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_" + type + "_off_right"), + new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_" + type + "_on_left"), + new ResourceLocation(ComputerCraftAPI.MOD_ID, "block/turtle_modem_" + type + "_on_right") + ); + } + + public Stream getDependencies() { + return Stream.of(leftOffModel, rightOffModel, leftOnModel, rightOnModel); + } } } diff --git a/projects/common/src/client/java/dan200/computercraft/client/turtle/TurtleUpgradeModellers.java b/projects/common/src/client/java/dan200/computercraft/client/turtle/TurtleUpgradeModellers.java index 7d62afc67..e15fc2bca 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/turtle/TurtleUpgradeModellers.java +++ b/projects/common/src/client/java/dan200/computercraft/client/turtle/TurtleUpgradeModellers.java @@ -75,6 +75,6 @@ public final class TurtleUpgradeModellers { public static Stream getDependencies() { fetchedModels = true; - return turtleModels.values().stream().flatMap(x -> x.getDependencies().stream()); + return turtleModels.values().stream().flatMap(TurtleUpgradeModeller::getDependencies); } } diff --git a/projects/common/src/generated/resources/data/computercraft/computercraft/pocket_upgrade/wireless_modem_advanced.json b/projects/common/src/generated/resources/data/computercraft/computercraft/pocket_upgrade/wireless_modem_advanced.json index 8d9466900..c7daaa3ed 100644 --- a/projects/common/src/generated/resources/data/computercraft/computercraft/pocket_upgrade/wireless_modem_advanced.json +++ b/projects/common/src/generated/resources/data/computercraft/computercraft/pocket_upgrade/wireless_modem_advanced.json @@ -1 +1 @@ -{"type": "computercraft:wireless_modem_advanced", "item": "computercraft:wireless_modem_advanced"} +{"type": "computercraft:wireless_modem", "advanced": true, "item": "computercraft:wireless_modem_advanced"} diff --git a/projects/common/src/generated/resources/data/computercraft/computercraft/pocket_upgrade/wireless_modem_normal.json b/projects/common/src/generated/resources/data/computercraft/computercraft/pocket_upgrade/wireless_modem_normal.json index 2c1a9cbd1..93c800e83 100644 --- a/projects/common/src/generated/resources/data/computercraft/computercraft/pocket_upgrade/wireless_modem_normal.json +++ b/projects/common/src/generated/resources/data/computercraft/computercraft/pocket_upgrade/wireless_modem_normal.json @@ -1 +1 @@ -{"type": "computercraft:wireless_modem_normal", "item": "computercraft:wireless_modem_normal"} +{"type": "computercraft:wireless_modem", "advanced": false, "item": "computercraft:wireless_modem_normal"} diff --git a/projects/common/src/generated/resources/data/computercraft/computercraft/turtle_upgrade/wireless_modem_advanced.json b/projects/common/src/generated/resources/data/computercraft/computercraft/turtle_upgrade/wireless_modem_advanced.json index 8d9466900..c7daaa3ed 100644 --- a/projects/common/src/generated/resources/data/computercraft/computercraft/turtle_upgrade/wireless_modem_advanced.json +++ b/projects/common/src/generated/resources/data/computercraft/computercraft/turtle_upgrade/wireless_modem_advanced.json @@ -1 +1 @@ -{"type": "computercraft:wireless_modem_advanced", "item": "computercraft:wireless_modem_advanced"} +{"type": "computercraft:wireless_modem", "advanced": true, "item": "computercraft:wireless_modem_advanced"} diff --git a/projects/common/src/generated/resources/data/computercraft/computercraft/turtle_upgrade/wireless_modem_normal.json b/projects/common/src/generated/resources/data/computercraft/computercraft/turtle_upgrade/wireless_modem_normal.json index 2c1a9cbd1..93c800e83 100644 --- a/projects/common/src/generated/resources/data/computercraft/computercraft/turtle_upgrade/wireless_modem_normal.json +++ b/projects/common/src/generated/resources/data/computercraft/computercraft/turtle_upgrade/wireless_modem_normal.json @@ -1 +1 @@ -{"type": "computercraft:wireless_modem_normal", "item": "computercraft:wireless_modem_normal"} +{"type": "computercraft:wireless_modem", "advanced": false, "item": "computercraft:wireless_modem_normal"} diff --git a/projects/common/src/main/java/dan200/computercraft/shared/ModRegistry.java b/projects/common/src/main/java/dan200/computercraft/shared/ModRegistry.java index 181891678..fad9f1b12 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/ModRegistry.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/ModRegistry.java @@ -392,10 +392,8 @@ public final class ModRegistry { REGISTRY.register("speaker", () -> UpgradeType.simpleWithCustomItem(TurtleSpeaker::new)); public static final RegistryEntry> WORKBENCH = REGISTRY.register("workbench", () -> UpgradeType.simpleWithCustomItem(TurtleCraftingTable::new)); - public static final RegistryEntry> WIRELESS_MODEM_NORMAL = - REGISTRY.register("wireless_modem_normal", () -> UpgradeType.simpleWithCustomItem(item -> new TurtleModem(item, false))); - public static final RegistryEntry> WIRELESS_MODEM_ADVANCED = - REGISTRY.register("wireless_modem_advanced", () -> UpgradeType.simpleWithCustomItem(item -> new TurtleModem(item, true))); + public static final RegistryEntry> WIRELESS_MODEM = + REGISTRY.register("wireless_modem", () -> UpgradeType.create(TurtleModem.CODEC)); public static final RegistryEntry> TOOL = REGISTRY.register("tool", () -> UpgradeType.create(TurtleTool.CODEC)); } @@ -405,10 +403,7 @@ public final class ModRegistry { public static final RegistryEntry> SPEAKER = REGISTRY.register("speaker", () -> UpgradeType.simpleWithCustomItem(PocketSpeaker::new)); - public static final RegistryEntry> WIRELESS_MODEM_NORMAL = - REGISTRY.register("wireless_modem_normal", () -> UpgradeType.simpleWithCustomItem(item -> new PocketModem(item, false))); - public static final RegistryEntry> WIRELESS_MODEM_ADVANCED = - REGISTRY.register("wireless_modem_advanced", () -> UpgradeType.simpleWithCustomItem(item -> new PocketModem(item, true))); + public static final RegistryEntry> WIRELESS_MODEM = REGISTRY.register("wireless_modem", () -> UpgradeType.create(PocketModem.CODEC)); } public static class Menus { diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java index b23faa456..70dd0d96b 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java @@ -4,17 +4,26 @@ package dan200.computercraft.shared.pocket.peripherals; +import com.mojang.serialization.Codec; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.pocket.AbstractPocketUpgrade; import dan200.computercraft.api.pocket.IPocketAccess; import dan200.computercraft.api.upgrades.UpgradeType; import dan200.computercraft.shared.ModRegistry; import dan200.computercraft.shared.peripheral.modem.wireless.WirelessModemPeripheral; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.world.item.ItemStack; import javax.annotation.Nullable; public class PocketModem extends AbstractPocketUpgrade { + public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( + BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter(x -> x.getCraftingItem().getItem()), + Codec.BOOL.fieldOf("advanced").forGetter(x -> x.advanced) + ).apply(instance, (item, advanced) -> new PocketModem(new ItemStack(item), advanced))); + private final boolean advanced; public PocketModem(ItemStack stack, boolean advanced) { @@ -40,8 +49,6 @@ public class PocketModem extends AbstractPocketUpgrade { @Override public UpgradeType getType() { - return advanced - ? ModRegistry.PocketUpgradeTypes.WIRELESS_MODEM_ADVANCED.get() - : ModRegistry.PocketUpgradeTypes.WIRELESS_MODEM_NORMAL.get(); + return ModRegistry.PocketUpgradeTypes.WIRELESS_MODEM.get(); } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java index 547118406..6af5efe60 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java @@ -4,6 +4,9 @@ package dan200.computercraft.shared.turtle.upgrades; +import com.mojang.serialization.Codec; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.turtle.*; import dan200.computercraft.api.upgrades.UpgradeType; @@ -12,6 +15,7 @@ import dan200.computercraft.shared.peripheral.modem.ModemState; import dan200.computercraft.shared.peripheral.modem.wireless.WirelessModemPeripheral; import net.minecraft.core.Direction; import net.minecraft.core.component.DataComponentPatch; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; @@ -19,6 +23,11 @@ import net.minecraft.world.phys.Vec3; import javax.annotation.Nullable; public class TurtleModem extends AbstractTurtleUpgrade { + public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( + BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter(x -> x.getCraftingItem().getItem()), + Codec.BOOL.fieldOf("advanced").forGetter(TurtleModem::advanced) + ).apply(instance, (item, advanced) -> new TurtleModem(new ItemStack(item), advanced))); + private static class Peripheral extends WirelessModemPeripheral { private final ITurtleAccess turtle; @@ -55,6 +64,10 @@ public class TurtleModem extends AbstractTurtleUpgrade { this.advanced = advanced; } + public boolean advanced() { + return advanced; + } + @Override public IPeripheral createPeripheral(ITurtleAccess turtle, TurtleSide side) { return new Peripheral(turtle, advanced); @@ -86,8 +99,6 @@ public class TurtleModem extends AbstractTurtleUpgrade { @Override public UpgradeType getType() { - return advanced - ? ModRegistry.TurtleUpgradeTypes.WIRELESS_MODEM_ADVANCED.get() - : ModRegistry.TurtleUpgradeTypes.WIRELESS_MODEM_NORMAL.get(); + return ModRegistry.TurtleUpgradeTypes.WIRELESS_MODEM.get(); } }