Use a single upgrade type for modems

Replace turtle_modem_{normal,advanced} with a single turtle_modem
upgrade (and likewise for pocket upgrades). This is slightly more
complex (we now need a custom codec), but I think is more idiomatic.
This commit is contained in:
Jonathan Coates 2024-04-28 20:38:30 +01:00
parent 06ac373e83
commit 959bdaeb61
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
11 changed files with 67 additions and 54 deletions

View File

@ -13,8 +13,7 @@
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<T extends ITurtleUpgrade> {
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.
* <p>
* 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<ResourceLocation> getDependencies() {
return List.of();
default Stream<ResourceLocation> getDependencies() {
return Stream.of();
}
/**
@ -85,8 +84,8 @@ public TransformedModel getModel(T upgrade, @Nullable ITurtleAccess turtle, Turt
}
@Override
public Collection<ResourceLocation> getDependencies() {
return List.of(left, right);
public Stream<ResourceLocation> getDependencies() {
return Stream.of(left, right);
}
};
}

View File

@ -125,8 +125,7 @@ public static void registerTurtleModellers(RegisterTurtleUpgradeModeller registe
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());
}

View File

@ -15,44 +15,46 @@
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<TurtleModem> {
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<ResourceLocation> getDependencies() {
return List.of(leftOffModel, rightOffModel, leftOnModel, rightOnModel);
public Stream<ResourceLocation> 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<ResourceLocation> getDependencies() {
return Stream.of(leftOffModel, rightOffModel, leftOnModel, rightOnModel);
}
}
}

View File

@ -75,6 +75,6 @@ private static TurtleUpgradeModeller<?> getModeller(ITurtleUpgrade upgrade) {
public static Stream<ResourceLocation> getDependencies() {
fetchedModels = true;
return turtleModels.values().stream().flatMap(x -> x.getDependencies().stream());
return turtleModels.values().stream().flatMap(TurtleUpgradeModeller::getDependencies);
}
}

View File

@ -1 +1 @@
{"type": "computercraft:wireless_modem_advanced", "item": "computercraft:wireless_modem_advanced"}
{"type": "computercraft:wireless_modem", "advanced": true, "item": "computercraft:wireless_modem_advanced"}

View File

@ -1 +1 @@
{"type": "computercraft:wireless_modem_normal", "item": "computercraft:wireless_modem_normal"}
{"type": "computercraft:wireless_modem", "advanced": false, "item": "computercraft:wireless_modem_normal"}

View File

@ -1 +1 @@
{"type": "computercraft:wireless_modem_advanced", "item": "computercraft:wireless_modem_advanced"}
{"type": "computercraft:wireless_modem", "advanced": true, "item": "computercraft:wireless_modem_advanced"}

View File

@ -1 +1 @@
{"type": "computercraft:wireless_modem_normal", "item": "computercraft:wireless_modem_normal"}
{"type": "computercraft:wireless_modem", "advanced": false, "item": "computercraft:wireless_modem_normal"}

View File

@ -392,10 +392,8 @@ public static class TurtleUpgradeTypes {
REGISTRY.register("speaker", () -> UpgradeType.simpleWithCustomItem(TurtleSpeaker::new));
public static final RegistryEntry<UpgradeType<TurtleCraftingTable>> WORKBENCH =
REGISTRY.register("workbench", () -> UpgradeType.simpleWithCustomItem(TurtleCraftingTable::new));
public static final RegistryEntry<UpgradeType<TurtleModem>> WIRELESS_MODEM_NORMAL =
REGISTRY.register("wireless_modem_normal", () -> UpgradeType.simpleWithCustomItem(item -> new TurtleModem(item, false)));
public static final RegistryEntry<UpgradeType<TurtleModem>> WIRELESS_MODEM_ADVANCED =
REGISTRY.register("wireless_modem_advanced", () -> UpgradeType.simpleWithCustomItem(item -> new TurtleModem(item, true)));
public static final RegistryEntry<UpgradeType<TurtleModem>> WIRELESS_MODEM =
REGISTRY.register("wireless_modem", () -> UpgradeType.create(TurtleModem.CODEC));
public static final RegistryEntry<UpgradeType<TurtleTool>> TOOL = REGISTRY.register("tool", () -> UpgradeType.create(TurtleTool.CODEC));
}
@ -405,10 +403,7 @@ public static class PocketUpgradeTypes {
public static final RegistryEntry<UpgradeType<PocketSpeaker>> SPEAKER =
REGISTRY.register("speaker", () -> UpgradeType.simpleWithCustomItem(PocketSpeaker::new));
public static final RegistryEntry<UpgradeType<PocketModem>> WIRELESS_MODEM_NORMAL =
REGISTRY.register("wireless_modem_normal", () -> UpgradeType.simpleWithCustomItem(item -> new PocketModem(item, false)));
public static final RegistryEntry<UpgradeType<PocketModem>> WIRELESS_MODEM_ADVANCED =
REGISTRY.register("wireless_modem_advanced", () -> UpgradeType.simpleWithCustomItem(item -> new PocketModem(item, true)));
public static final RegistryEntry<UpgradeType<PocketModem>> WIRELESS_MODEM = REGISTRY.register("wireless_modem", () -> UpgradeType.create(PocketModem.CODEC));
}
public static class Menus {

View File

@ -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<PocketModem> 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 void update(IPocketAccess access, @Nullable IPeripheral peripheral) {
@Override
public UpgradeType<PocketModem> getType() {
return advanced
? ModRegistry.PocketUpgradeTypes.WIRELESS_MODEM_ADVANCED.get()
: ModRegistry.PocketUpgradeTypes.WIRELESS_MODEM_NORMAL.get();
return ModRegistry.PocketUpgradeTypes.WIRELESS_MODEM.get();
}
}

View File

@ -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.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 javax.annotation.Nullable;
public class TurtleModem extends AbstractTurtleUpgrade {
public static final MapCodec<TurtleModem> 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 TurtleModem(ItemStack stack, boolean advanced) {
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 DataComponentPatch getPersistedData(DataComponentPatch upgradeData) {
@Override
public UpgradeType<TurtleModem> getType() {
return advanced
? ModRegistry.TurtleUpgradeTypes.WIRELESS_MODEM_ADVANCED.get()
: ModRegistry.TurtleUpgradeTypes.WIRELESS_MODEM_NORMAL.get();
return ModRegistry.TurtleUpgradeTypes.WIRELESS_MODEM.get();
}
}