From 15cdc39ec2f500642a9c5efcebb484b8f2121e48 Mon Sep 17 00:00:00 2001 From: Mr-Zombii Date: Mon, 15 Apr 2024 17:51:35 -0500 Subject: [PATCH] - Fixed StringID settings in BlockGenerator - Added Documentation for 15+ classes - Modified the AssetLoader to check if the asset exists --- .../api/generators/BlockEventGenerator.java | 13 +++++++++++ .../flux/api/generators/BlockGenerator.java | 5 +---- .../api/generators/BlockStateGenerator.java | 15 +++++++++++++ .../flux/api/generators/data/DataJson.java | 3 +++ .../data/blockevent/BlockEventData.java | 9 ++++++++ .../data/blockevent/BlockEventDataExt.java | 3 +++ .../data/blockevent/BlockEventType.java | 3 +++ .../data/blockevent/triggers/TriggerData.java | 6 +++++ .../blockevent/triggers/TriggerEventData.java | 7 ++++++ .../data/blockstate/BlockStateData.java | 6 +++++ .../data/blockstate/BlockStateDataExt.java | 4 ++++ .../api/gui/interfaces/GameStateCache.java | 7 +++++- .../gui/interfaces/GameStateInterface.java | 3 +++ .../flux/api/resource/AssetLoader.java | 22 +++++++++++++++++-- .../flux/api/resource/ResourceLocation.java | 7 ++++++ .../flux/api/resource/ResourceObject.java | 11 +++++++++- 16 files changed, 116 insertions(+), 8 deletions(-) diff --git a/src/main/java/dev/crmodders/flux/api/generators/BlockEventGenerator.java b/src/main/java/dev/crmodders/flux/api/generators/BlockEventGenerator.java index d0c7b5b..23df8ad 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/BlockEventGenerator.java +++ b/src/main/java/dev/crmodders/flux/api/generators/BlockEventGenerator.java @@ -29,12 +29,25 @@ import java.util.*; +/** + * An internal class used in {@link BlockGenerator} to ease the creation of blockEvents. + */ + @Stable public class BlockEventGenerator { + /** + * The action instances for every {@link IBlockAction} + */ public static Map ALL_ACTION_INSTANCES = new HashMap<>(); + /** + * An action to register a blockEventAction without the pain of Annotations introduces in CosmicReach 0.1.22. + * + * @param id The identifier for the blockAction that can be called in triggers using the actionId. + * @param action The action to execute in during a trigger event. + */ public static void registerBlockEventAction(Identifier id, IBlockAction action) { ALL_ACTION_INSTANCES.put(id.toString(), action); BlockEvents.ALL_ACTIONS.put(id.toString(), action.getClass()); diff --git a/src/main/java/dev/crmodders/flux/api/generators/BlockGenerator.java b/src/main/java/dev/crmodders/flux/api/generators/BlockGenerator.java index 8251219..6f9072d 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/BlockGenerator.java +++ b/src/main/java/dev/crmodders/flux/api/generators/BlockGenerator.java @@ -1,15 +1,12 @@ package dev.crmodders.flux.api.generators; import com.badlogic.gdx.files.FileHandle; -import com.badlogic.gdx.utils.Json; import dev.crmodders.flux.annotations.Stable; import dev.crmodders.flux.api.block.IModBlock; -import dev.crmodders.flux.api.events.GameEvents; import dev.crmodders.flux.api.generators.data.blockevent.BlockEventType; import dev.crmodders.flux.api.generators.data.blockstate.BlockStateData; import dev.crmodders.flux.api.generators.data.blockstate.BlockStateDataExt; import dev.crmodders.flux.api.generators.suppliers.BasicTriggerSupplier; -import dev.crmodders.flux.api.resource.AssetLoader; import dev.crmodders.flux.api.resource.ResourceLocation; import dev.crmodders.flux.api.suppliers.ReturnableDoubleInputSupplier; import dev.crmodders.flux.registry.FluxRegistries; @@ -188,7 +185,7 @@ public ReturnableDoubleInputSupplier { if (!FluxRegistries.BLOCKS.isFrozen()) throw new RuntimeException("CANNOT USE GENERATOR FACTORY BECAUSE REGISTRIES ARE NOT FROZEN YET"); - if (object.get("stringId") != null) object.set("stringId", id.toString()); + if (object.get("stringId") == null) object.set("stringId", id.toString()); FileHandle dataBlock = null; try { diff --git a/src/main/java/dev/crmodders/flux/api/generators/BlockStateGenerator.java b/src/main/java/dev/crmodders/flux/api/generators/BlockStateGenerator.java index e9b5338..ab9ff77 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/BlockStateGenerator.java +++ b/src/main/java/dev/crmodders/flux/api/generators/BlockStateGenerator.java @@ -8,15 +8,30 @@ import dev.crmodders.flux.tags.Identifier; import org.hjson.JsonObject; +/** + * The internal generation class for {@link BlockStateGenerator} + */ + @Stable public class BlockStateGenerator { protected JsonObject object; + /** + * Creates a BlockStateGenerator + */ public static BlockStateGenerator createGenerator() { return new BlockStateGenerator(); } + /** + * Creates a {@link BlockStateData} based on 2 params + * + * @param blockEventsId The Event to be used when this blockState is active. + * @param modelName The model that is displayed when this state is loaded. + * + * @return A newly created BlockState + */ public static BlockStateData createBasicBlockState(Identifier blockEventsId, String modelName) { return new BlockStateData( blockEventsId, modelName diff --git a/src/main/java/dev/crmodders/flux/api/generators/data/DataJson.java b/src/main/java/dev/crmodders/flux/api/generators/data/DataJson.java index dc00fc7..2cddff2 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/data/DataJson.java +++ b/src/main/java/dev/crmodders/flux/api/generators/data/DataJson.java @@ -2,6 +2,9 @@ import org.hjson.JsonObject; +/** + * The Base interface for any item that needs to be converted to json. + */ public interface DataJson { JsonObject toJson(); diff --git a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventData.java b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventData.java index d99c575..735dc36 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventData.java +++ b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventData.java @@ -5,6 +5,15 @@ import dev.crmodders.flux.tags.Identifier; import org.hjson.JsonObject; +/** + * The Structure of blockEvents used in {@link dev.crmodders.flux.api.generators.BlockEventGenerator}. + * + * @param parentEvent the parent event to base the events off of. + * @param identifier the id for this block event. + * @param triggers the triggers called during certain interactions with this event + * + * @param noParent this just disables parentEvent from being written + */ public record BlockEventData( Identifier parentEvent, Identifier identifier, diff --git a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventDataExt.java b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventDataExt.java index 4c3c0ca..90f0903 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventDataExt.java +++ b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventDataExt.java @@ -2,5 +2,8 @@ import dev.crmodders.flux.api.generators.data.DataJson; +/** + * The Base interface for {@link BlockEventData} + */ public interface BlockEventDataExt extends DataJson { } diff --git a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventType.java b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventType.java index 40f65bb..17bae24 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventType.java +++ b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/BlockEventType.java @@ -1,5 +1,8 @@ package dev.crmodders.flux.api.generators.data.blockevent; +/** + * The trigger types provided to be used as labels for custom programmed triggers in {@link dev.crmodders.flux.api.generators.BlockGenerator} and {@link dev.crmodders.flux.api.generators.BlockEventGenerator} + */ public enum BlockEventType { OnInteract, OnPlace, diff --git a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/triggers/TriggerData.java b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/triggers/TriggerData.java index 76cdba7..e81e686 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/triggers/TriggerData.java +++ b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/triggers/TriggerData.java @@ -5,6 +5,12 @@ import org.hjson.JsonArray; import org.hjson.JsonObject; +/** + * The interaction trigger that activates the other events listed in the events parameter. + * + * @param name the interaction name of this trigger. + * @param events The trigger events that are activated as soon as this trigger is loaded after interaction. + */ public record TriggerData( String name, TriggerEventData[] events diff --git a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/triggers/TriggerEventData.java b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/triggers/TriggerEventData.java index 2aeed0c..4948aae 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/triggers/TriggerEventData.java +++ b/src/main/java/dev/crmodders/flux/api/generators/data/blockevent/triggers/TriggerEventData.java @@ -3,6 +3,7 @@ import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import dev.crmodders.flux.api.generators.data.DataJson; +import dev.crmodders.flux.api.generators.data.blockevent.BlockEventData; import dev.crmodders.flux.api.suppliers.ReturnableSupplier; import dev.crmodders.flux.tags.Identifier; import org.hjson.JsonObject; @@ -12,6 +13,12 @@ import java.util.HashMap; import java.util.Map; +/** + * The trigger activated during the parent Interation Trigger + * + * @param actionID the code-based-action loaded on activation + * @param parameters the params passed into the action from actionID + */ public record TriggerEventData( Identifier actionID, Map parameters diff --git a/src/main/java/dev/crmodders/flux/api/generators/data/blockstate/BlockStateData.java b/src/main/java/dev/crmodders/flux/api/generators/data/blockstate/BlockStateData.java index 95fc00d..654d83e 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/data/blockstate/BlockStateData.java +++ b/src/main/java/dev/crmodders/flux/api/generators/data/blockstate/BlockStateData.java @@ -3,6 +3,12 @@ import dev.crmodders.flux.tags.Identifier; import org.hjson.JsonObject; +/** + * A datastructure built for making basic blockStates from events and models. + * + * @param blockEventsId The Event to be used when this blockState is active. + * @param modelName The model that is displayed when this state is loaded. + */ public record BlockStateData( Identifier blockEventsId, String modelName diff --git a/src/main/java/dev/crmodders/flux/api/generators/data/blockstate/BlockStateDataExt.java b/src/main/java/dev/crmodders/flux/api/generators/data/blockstate/BlockStateDataExt.java index 43afc25..b413da9 100644 --- a/src/main/java/dev/crmodders/flux/api/generators/data/blockstate/BlockStateDataExt.java +++ b/src/main/java/dev/crmodders/flux/api/generators/data/blockstate/BlockStateDataExt.java @@ -2,6 +2,10 @@ import dev.crmodders.flux.api.generators.data.DataJson; + +/** + * The Base interface for {@link BlockStateData} + */ public interface BlockStateDataExt extends DataJson { } diff --git a/src/main/java/dev/crmodders/flux/api/gui/interfaces/GameStateCache.java b/src/main/java/dev/crmodders/flux/api/gui/interfaces/GameStateCache.java index a7593cf..3f1ae47 100644 --- a/src/main/java/dev/crmodders/flux/api/gui/interfaces/GameStateCache.java +++ b/src/main/java/dev/crmodders/flux/api/gui/interfaces/GameStateCache.java @@ -4,9 +4,14 @@ import java.util.List; +import finalforeach.cosmicreach.blocks.Block; + +/** + * The basic cache for gameStates that holds the current block state and some other methods you can use in your impls. + */ public interface GameStateCache { - public static GameStateCache getCache() { + static GameStateCache getCache() { return (GameStateCache) GameState.currentGameState; } diff --git a/src/main/java/dev/crmodders/flux/api/gui/interfaces/GameStateInterface.java b/src/main/java/dev/crmodders/flux/api/gui/interfaces/GameStateInterface.java index 8abd40b..f79f5da 100644 --- a/src/main/java/dev/crmodders/flux/api/gui/interfaces/GameStateInterface.java +++ b/src/main/java/dev/crmodders/flux/api/gui/interfaces/GameStateInterface.java @@ -6,6 +6,9 @@ import java.util.List; +/** + * A basic gameState structure for all states to add some getters + */ public interface GameStateInterface { Viewport getViewport(); diff --git a/src/main/java/dev/crmodders/flux/api/resource/AssetLoader.java b/src/main/java/dev/crmodders/flux/api/resource/AssetLoader.java index 3326a65..1a7e3fd 100644 --- a/src/main/java/dev/crmodders/flux/api/resource/AssetLoader.java +++ b/src/main/java/dev/crmodders/flux/api/resource/AssetLoader.java @@ -15,6 +15,12 @@ public class AssetLoader { + /** + * A method to load assets that outputs a RegistryObject that when called will load the object from the registry. + * @param key the resource location. + * + * @return the registry object to possibly make the registry safer from null ptrs. + */ public static RegistryObject loadAsset(ResourceLocation key) { ResourceObject resourceObject = new ResourceObject( @@ -23,7 +29,7 @@ public static RegistryObject loadAsset(ResourceLocation key) { ); if (FluxConstants.GameHasLoaded) - resourceObject.handle = GameAssetLoader.loadAsset(key.toString()); + resourceObject.handle = unsafeLoadAsset(key).handle; return FluxRegistries.GAME_RESOURCES.register( key, @@ -32,6 +38,12 @@ public static RegistryObject loadAsset(ResourceLocation key) { } + /** + * A method to load the assets mentioned by the key param that only works if the asset exists and the handle may be null in certain situations. + * @param key the loaded resource. + * + * @return the resource object that holds the key and FileHandle. + */ public static ResourceObject unsafeLoadAsset(ResourceLocation key) { ResourceObject resourceObject = new ResourceObject( @@ -40,12 +52,18 @@ public static ResourceObject unsafeLoadAsset(ResourceLocation key) { ); if (assetExists(key)) - resourceObject.handle = GameAssetLoader.loadAsset(key.toString()); + resourceObject.handle = key.load(); return resourceObject; } + /** + * A method to check if a resource exists. + * @param location the resource location to check. + * + * @return the condition saying if the asset exists or not. + */ public static boolean assetExists(ResourceLocation location) { FileHandle modLocationFile = Gdx.files.absolute(SaveLocation.getSaveFolderLocation() + "/mods/assets/" + location.namespace); if (modLocationFile.exists()) return true; diff --git a/src/main/java/dev/crmodders/flux/api/resource/ResourceLocation.java b/src/main/java/dev/crmodders/flux/api/resource/ResourceLocation.java index 7e78229..5b36f79 100644 --- a/src/main/java/dev/crmodders/flux/api/resource/ResourceLocation.java +++ b/src/main/java/dev/crmodders/flux/api/resource/ResourceLocation.java @@ -22,10 +22,17 @@ public static ResourceLocation fromString(String id) { return new ResourceLocation(splitId[0], splitId[1]); } + /** + * A method to load the resource mentioned in the constructor. + * @param forceReload it replaces the currently loaded resource with a reload of it. + */ public FileHandle load(boolean forceReload) { return GameAssetLoader.loadAsset(toString(), forceReload); } + /** + * A method to load the current resource mentioned in the creation construction. + */ public FileHandle load() { return load(false); } diff --git a/src/main/java/dev/crmodders/flux/api/resource/ResourceObject.java b/src/main/java/dev/crmodders/flux/api/resource/ResourceObject.java index a04b624..6d591d3 100644 --- a/src/main/java/dev/crmodders/flux/api/resource/ResourceObject.java +++ b/src/main/java/dev/crmodders/flux/api/resource/ResourceObject.java @@ -2,12 +2,21 @@ import com.badlogic.gdx.files.FileHandle; +import javax.annotation.Nullable; + +/** + * A resource object that holds its location and file handle + */ public class ResourceObject { public ResourceLocation key; public FileHandle handle; - public ResourceObject(ResourceLocation key, FileHandle handle) { + /** + * @param key the location for the resource + * @param handle the handle that may be null depending on where your mod is loaded or where this method is used during init. + */ + public ResourceObject(ResourceLocation key, @Nullable FileHandle handle) { this.key = key; this.handle = handle; }