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

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

98 lines
4.3 KiB
Java
Raw Normal View History

// SPDX-FileCopyrightText: 2021 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
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
package dan200.computercraft.api.upgrades;
import com.google.gson.JsonObject;
2024-01-31 20:55:14 +00:00
import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.api.pocket.PocketUpgradeDataProvider;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleUpgradeDataProvider;
import dan200.computercraft.impl.upgrades.SerialiserWithCraftingItem;
import dan200.computercraft.impl.upgrades.SimpleSerialiser;
import net.minecraft.core.Registry;
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;
2024-01-31 20:55:14 +00:00
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.SimpleCraftingRecipeSerializer;
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
2024-01-31 20:55:14 +00:00
import java.util.function.BiFunction;
import java.util.function.Function;
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
/**
2024-01-31 20:55:14 +00:00
* A serialiser for {@link ITurtleUpgrade} or {@link IPocketUpgrade}s.
* <p>
2024-01-31 20:55:14 +00:00
* These should be registered in a {@link Registry} while the game is loading, much like {@link RecipeSerializer}s.
* <p>
* This interface is very similar to {@link RecipeSerializer}; each serialiser should correspond to a specific upgrade
* class. Upgrades are then read from JSON files in datapacks, allowing multiple instances of the upgrade to be
* registered.
* <p>
* If your upgrade doesn't have any associated configurable parameters (like most upgrades), you can use
* {@link #simple(Function)} or {@link #simpleWithCustomItem(BiFunction)} to create a basic upgrade serialiser.
* <p>
* Upgrades may be data generated via a {@link UpgradeDataProvider} (see {@link TurtleUpgradeDataProvider} and
* {@link PocketUpgradeDataProvider}).
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
*
* @param <T> The upgrade that this class can serialise and deserialise.
2024-01-31 20:55:14 +00:00
* @see ITurtleUpgrade
* @see IPocketUpgrade
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
*/
public interface UpgradeSerialiser<T extends 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
/**
* Read this upgrade from a JSON file in a datapack.
*
* @param id The ID of this upgrade.
* @param object The JSON object to load this upgrade from.
* @return The constructed upgrade, with a {@link UpgradeBase#getUpgradeID()} equal to {@code id}.
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
* @see net.minecraft.util.GsonHelper For additional JSON helper methods.
*/
2022-11-06 15:50:24 +00:00
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
/**
* Read this upgrade from a network packet, sent from the server.
*
* @param id The ID of this upgrade.
* @param buffer The buffer object to read this upgrade from.
* @return The constructed upgrade, with a {@link UpgradeBase#getUpgradeID()} equal to {@code id}.
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
*/
T fromNetwork(ResourceLocation id, RegistryFriendlyByteBuf 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
/**
* Write this upgrade to a network packet, to be sent to the client.
*
* @param buffer The buffer object to write this upgrade to
* @param upgrade The upgrade to write.
*/
void toNetwork(RegistryFriendlyByteBuf buffer, T upgrade);
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
2024-01-31 20:55:14 +00:00
/**
* Create an upgrade serialiser for a simple upgrade. This is similar to a {@link SimpleCraftingRecipeSerializer},
* but for upgrades.
* <p>
* If you might want to vary the item, it's suggested you use {@link #simpleWithCustomItem(BiFunction)} instead.
*
* @param factory Generate a new upgrade with a specific ID.
* @param <T> The type of the generated upgrade.
* @return The serialiser for this upgrade
*/
static <T extends UpgradeBase> UpgradeSerialiser<T> simple(Function<ResourceLocation, T> factory) {
return new SimpleSerialiser<>(factory);
}
/**
* Create an upgrade serialiser for a simple upgrade whose crafting item can be specified.
*
* @param factory Generate a new upgrade with a specific ID and crafting item. The returned upgrade's
* {@link UpgradeBase#getCraftingItem()} <strong>MUST</strong> equal the provided item.
* @param <T> The type of the generated upgrade.
* @return The serialiser for this upgrade.
* @see #simple(Function) For upgrades whose crafting stack should not vary.
*/
static <T extends UpgradeBase> UpgradeSerialiser<T> simpleWithCustomItem(BiFunction<ResourceLocation, ItemStack, T> factory) {
return new SerialiserWithCraftingItem<>(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
}