Skip to content

Commit

Permalink
- Fixed StringID settings in BlockGenerator
Browse files Browse the repository at this point in the history
- Added Documentation for 15+ classes
- Modified the AssetLoader to check if the asset exists
  • Loading branch information
Mr-Zombii committed Apr 15, 2024
1 parent 5e7dcf3 commit 15cdc39
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, IBlockAction> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -188,7 +185,7 @@ public ReturnableDoubleInputSupplier<IModBlock, Identifier, FactoryFinalizer<Blo

return (block, id) -> {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

import dev.crmodders.flux.api.generators.data.DataJson;

/**
* The Base interface for {@link BlockEventData}
*/
public interface BlockEventDataExt extends DataJson {
}
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Object> parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import dev.crmodders.flux.api.generators.data.DataJson;


/**
* The Base interface for {@link BlockStateData}
*/
public interface BlockStateDataExt extends DataJson {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import java.util.List;

/**
* A basic gameState structure for all states to add some getters
*/
public interface GameStateInterface {
Viewport getViewport();

Expand Down
22 changes: 20 additions & 2 deletions src/main/java/dev/crmodders/flux/api/resource/AssetLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceObject> loadAsset(ResourceLocation key) {

ResourceObject resourceObject = new ResourceObject(
Expand All @@ -23,7 +29,7 @@ public static RegistryObject<ResourceObject> loadAsset(ResourceLocation key) {
);

if (FluxConstants.GameHasLoaded)
resourceObject.handle = GameAssetLoader.loadAsset(key.toString());
resourceObject.handle = unsafeLoadAsset(key).handle;

return FluxRegistries.GAME_RESOURCES.register(
key,
Expand All @@ -32,6 +38,12 @@ public static RegistryObject<ResourceObject> 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(
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 15cdc39

Please sign in to comment.