Skip to content

Commit

Permalink
Use a single upgrade type for modems
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SquidDev committed Apr 28, 2024
1 parent 06ac373 commit 959bdae
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,13 +15,19 @@
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;

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;

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

0 comments on commit 959bdae

Please sign in to comment.