-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
projects/pswg_blasters/src/main/java/dev/pswg/data/BlasterDataReloadListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package dev.pswg.data; | ||
|
||
import com.google.gson.JsonParser; | ||
import com.mojang.serialization.JsonOps; | ||
import dev.pswg.Blasters; | ||
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; | ||
import net.minecraft.resource.ResourceManager; | ||
import net.minecraft.util.Identifier; | ||
import org.apache.commons.io.FilenameUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* A datapack loader for blaster definitions | ||
*/ | ||
public class BlasterDataReloadListener implements SimpleSynchronousResourceReloadListener | ||
{ | ||
/** | ||
* The instance of the listener | ||
*/ | ||
public static final BlasterDataReloadListener INSTANCE = new BlasterDataReloadListener(); | ||
|
||
/** | ||
* The datapack folder in which this data resides | ||
*/ | ||
public static final String FOLDER = "blasters"; | ||
|
||
/** | ||
* The set of blaster definitions currently associated with the loaded world | ||
*/ | ||
private final HashMap<Identifier, BlasterDatapackDefinition> definitions = new HashMap<>(); | ||
|
||
private BlasterDataReloadListener() | ||
{ | ||
} | ||
|
||
/** | ||
* Gets the current set of blaster definitions associated with the loaded | ||
* world, keyed by the identifier deriving from their filename | ||
*/ | ||
public HashMap<Identifier, BlasterDatapackDefinition> getDefinitions() | ||
{ | ||
return definitions; | ||
} | ||
|
||
@Override | ||
public Identifier getFabricId() | ||
{ | ||
return Blasters.id("data"); | ||
} | ||
|
||
@Override | ||
public void reload(ResourceManager manager) | ||
{ | ||
definitions.clear(); | ||
|
||
for (var entry : manager.findResources(FOLDER, path -> path.getPath().endsWith(".json")).entrySet()) | ||
{ | ||
var key = entry.getKey(); | ||
var resource = entry.getValue(); | ||
|
||
try ( | ||
var stream = resource.getInputStream(); | ||
var reader = new InputStreamReader(stream) | ||
) | ||
{ | ||
definitions.put( | ||
key.withPath(FilenameUtils.getBaseName(key.getPath())), | ||
BlasterDatapackDefinition.CODEC | ||
.parse(JsonOps.INSTANCE, JsonParser.parseReader(reader)) | ||
.result() | ||
.orElseThrow(() -> new IOException("Failed to decode blaster definition from JSON")) | ||
); | ||
} | ||
catch (Exception e) | ||
{ | ||
Blasters.LOGGER.error("Failed to load data from blaster definition " + key, e); | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
projects/pswg_blasters/src/main/java/dev/pswg/data/BlasterDatapackDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.pswg.data; | ||
|
||
import dev.pswg.codecgenerator.GenerateCodec; | ||
import dev.pswg.codecgenerator.SelfCodec; | ||
import dev.pswg.generated.codecs.IBlasterDatapackDefinitionCodec; | ||
import dev.pswg.item.BlasterItem; | ||
|
||
/** | ||
* Defines the format for a blaster datapack JSON entry | ||
* | ||
* @param stats The stats the blaster preset will use | ||
* @param attachments The attachments the blaster preset will use | ||
*/ | ||
@GenerateCodec | ||
public record BlasterDatapackDefinition( | ||
@SelfCodec BlasterItem.StatsComponent stats, | ||
@SelfCodec BlasterItem.AttachmentsComponent attachments | ||
) implements IBlasterDatapackDefinitionCodec | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
projects/pswg_blasters/src/main/resources/data/pswg_blasters/blasters/test_blaster.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"stats": { | ||
"damage": 8, | ||
"range": 48, | ||
"automaticRepeatDelay": 4, | ||
"heat": { | ||
"capacity": 100, | ||
"perRound": 20, | ||
"drainSpeed": 5.0, | ||
"overheatPenalty": 60, | ||
"overheatDrainSpeed": 1, | ||
"passiveCooldownDelay": 20, | ||
"overchargeBonus": 40 | ||
}, | ||
"cooling": { | ||
"primaryBypassTime": 0.7, | ||
"primaryBypassTolerance": 0.1, | ||
"secondaryBypassTime": 0.25, | ||
"secondaryBypassTolerance": 0.05 | ||
} | ||
}, | ||
"attachments": { | ||
"hud": "pswg_blasters:default" | ||
} | ||
} |