Skip to content

Commit

Permalink
datapacked blaster definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
parzivail committed Nov 18, 2024
1 parent 318b161 commit 6391b9e
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 1 deletion.
17 changes: 16 additions & 1 deletion projects/pswg_blasters/src/main/java/dev/pswg/Blasters.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import dev.pswg.configuration.BlastersConfig;
import dev.pswg.configuration.IConfigContainer;
import dev.pswg.configuration.MemoryConfigContainer;
import dev.pswg.data.BlasterDataReloadListener;
import dev.pswg.entity.BlasterBoltEntity;
import dev.pswg.item.BlasterItem;
import dev.pswg.registry.Registrar;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroupEntries;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;

Expand Down Expand Up @@ -69,11 +73,22 @@ public static Identifier id(String path)
.trackingTickInterval(20)
);

private static void addBlastersToTab(FabricItemGroupEntries itemGroup)
{
for (var definition : BlasterDataReloadListener.INSTANCE.getDefinitions().entrySet())
{
LOGGER.debug("Registering blaster definition: {}", definition.getKey());
itemGroup.add(BlasterItem.createStack(definition.getKey(), definition.getValue()));
}
}

@Override
public void onGalaxiesReady()
{
ItemGroupEvents.modifyEntriesEvent(ItemGroups.COMBAT)
.register((itemGroup) -> itemGroup.add(BLASTER_ITEM));
.register(Blasters::addBlastersToTab);

ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(BlasterDataReloadListener.INSTANCE);

// TODO: how to differentiate different modules' versions?
LOGGER.info("Module initialized");
Expand Down
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);
}
}
}
}
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
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.pswg.codec.GalaxiesCodecs;
import dev.pswg.codecgenerator.GenerateCodec;
import dev.pswg.codecgenerator.SelfCodec;
import dev.pswg.data.BlasterDatapackDefinition;
import dev.pswg.entity.BlasterBoltEntity;
import dev.pswg.generated.codecs.*;
import dev.pswg.generated.recordbuilders.IStateComponentBuilder;
Expand All @@ -25,13 +26,15 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.consume.UseAction;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
Expand All @@ -40,6 +43,7 @@
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import java.util.List;
import java.util.Optional;
import java.util.function.UnaryOperator;

Expand Down Expand Up @@ -300,6 +304,15 @@ public StateComponent withCooling(CoolingMode mode, long timestamp)
EntityAttributeModifier.Operation.ADD_MULTIPLIED_BASE
);

/**
* The component that contains the datapack registrar ID of the blaster
*/
private static final ComponentType<Identifier> ID = Registry.register(
Registries.DATA_COMPONENT_TYPE,
Blasters.id("id"),
ComponentType.<Identifier>builder().codec(Identifier.CODEC).packetCodec(Identifier.PACKET_CODEC).build()
);

/**
* The component that contains the mutable gameplay state of the blaster
*/
Expand Down Expand Up @@ -333,11 +346,31 @@ public StateComponent withCooling(CoolingMode mode, long timestamp)
public static Settings createSettings()
{
return new Settings()
.component(ID, Blasters.id("missingno"))
.component(STATS, StatsComponent.DEFAULT)
.component(ATTACHMENTS, AttachmentsComponent.DEFAULT)
.component(STATE, StateComponent.DEFAULT);
}

/**
* Creates a new ItemStack that represents the given {@link BlasterDatapackDefinition}.
*
* @param id The id of the blaster item.
* @param definition The definition that this stack should represent.
*
* @return A new stack configured with the given definition.
*/
public static ItemStack createStack(Identifier id, BlasterDatapackDefinition definition)
{
var stack = new ItemStack(Blasters.BLASTER_ITEM);

stack.set(ID, id);
stack.set(STATS, definition.stats());
stack.set(ATTACHMENTS, definition.attachments());

return stack;
}

public BlasterItem(Settings settings)
{
super(settings);
Expand Down Expand Up @@ -493,6 +526,12 @@ public static boolean canFire(World world, LivingEntity user, ItemStack stack)
return true;
}

@Override
public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type)
{
tooltip.add(Text.of(stack.get(ID)));
}

/**
* Calculates the current accumulated heat of the blaster based on the dissipation rate and the time passed since the last shot.
*
Expand Down
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"
}
}

0 comments on commit 6391b9e

Please sign in to comment.