CC-Tweaked/projects/common-api/src/main/java/dan200/computercraft/impl/upgrades/SerialiserWithCraftingItem....

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.7 KiB
Java
Raw Normal View History

// SPDX-FileCopyrightText: 2021 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.impl.upgrades;
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
import com.google.gson.JsonObject;
import dan200.computercraft.api.upgrades.UpgradeBase;
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
import dan200.computercraft.api.upgrades.UpgradeSerialiser;
import net.minecraft.network.RegistryFriendlyByteBuf;
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.ApiStatus;
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
import java.util.function.BiFunction;
/**
* Simple serialiser which returns a constant upgrade with a custom crafting item.
* <p>
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
* Do <strong>NOT</strong> directly reference this class. It exists for internal use by the API.
*
* @param <T> The upgrade that this class can serialise and deserialise.
*/
@ApiStatus.Internal
2024-01-31 20:55:14 +00:00
public final class SerialiserWithCraftingItem<T extends UpgradeBase> implements UpgradeSerialiser<T> {
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
private final BiFunction<ResourceLocation, ItemStack, T> factory;
2024-01-31 20:55:14 +00:00
public SerialiserWithCraftingItem(BiFunction<ResourceLocation, ItemStack, T> factory) {
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
this.factory = factory;
}
@Override
2024-01-31 20:55:14 +00:00
public T fromJson(ResourceLocation id, JsonObject object) {
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
var item = GsonHelper.getAsItem(object, "item");
return factory.apply(id, new ItemStack(item));
}
@Override
public T fromNetwork(ResourceLocation id, RegistryFriendlyByteBuf buffer) {
var item = ItemStack.STREAM_CODEC.decode(buffer);
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
return factory.apply(id, item);
}
@Override
public void toNetwork(RegistryFriendlyByteBuf buffer, T upgrade) {
ItemStack.STREAM_CODEC.encode(buffer, upgrade.getCraftingItem());
Rewrite turtle upgrade registration to be more data driven (#967) The feature nobody asked for, but we're getting anyway. Old way to register a turtle/pocket computer upgrade: ComputerCraftAPI.registerTurtleUpgrade(new MyUpgrade(new ResourceLocation("my_mod", "my_upgrade"))); New way to register a turtle/pocket computer upgrade: First, define a serialiser for your turtle upgrade type: static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, "my_mod" ); public static final RegistryObject<TurtleUpgradeSerialiser<MyUpgrade>> MY_UPGRADE = SERIALISERS.register( "my_upgrade", () -> TurtleUpgradeSerialiser.simple( MyUpgrade::new ) ); SERIALISERS.register(bus); // Call in your mod constructor. Now either create a JSON string or use a data generator to register your upgrades: class TurtleDataGenerator extends TurtleUpgradeDataProvider { @Override protected void addUpgrades( @Nonnull Consumer<Upgrade<TurtleUpgradeSerialiser<?>>> addUpgrade ) simple(new ResourceLocation("my_mod", my_upgrade"), MY_UPGRADE.get()).add(addUpgrade); } } See much better! In all seriousness, this does offer some benefits, namely that it's now possible to overwrite or create upgrades via datapacks. Actual changes: - Remove ComputerCraftAPI.register{Turtle,Pocket}Upgrade functions. - Instead add {Turtle,Pocket}UpgradeSerialiser classes, which are used to load upgrades from JSON files in datapacks, and then read/write them to network packets (much like recipe serialisers). - The upgrade registries now subscribe to datapack reload events. They find all JSON files in the data/$mod_id/computercraft/{turtle,pocket}_upgrades directories, parse them, and then register them as upgrades. Once datapacks have fully reloaded, these upgrades are then sent over the network to the client. - Add data generators for turtle and pocket computer upgrades, to make the creation of JSON files a bit easier. - Port all of CC:T's upgrades over to use the new system.
2021-11-26 23:36:02 +00:00
}
}