Skip to content

Commit

Permalink
Move various configs to data maps instead
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Jul 21, 2024
1 parent a364bed commit a7acf98
Show file tree
Hide file tree
Showing 27 changed files with 363 additions and 198 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"values": {
"minecraft:magma_block": {
"temperature": 800
},
"powah:blazing_crystal_block": {
"temperature": 2800
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"values": {
"#c:water": {
"temperature": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"values": {
"#c:lava": {
"temperature": 1000
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"values": {
"#c:lava": {
"energy_produced": 10000
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"values": {
"minecraft:blue_ice": {
"amount": 568,
"temperature": -17
},
"minecraft:ice": {
"amount": 48,
"temperature": -5
},
"minecraft:packed_ice": {
"amount": 192,
"temperature": -8
},
"minecraft:snow_block": {
"amount": 48,
"temperature": -3
},
"minecraft:snowball": {
"amount": 12,
"temperature": -3
},
"powah:dry_ice": {
"amount": 712,
"temperature": -32
}
}
}
24 changes: 9 additions & 15 deletions src/main/java/owmii/powah/Powah.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModList;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import net.neoforged.neoforge.common.NeoForge;
Expand All @@ -26,7 +23,10 @@
import net.neoforged.neoforge.registries.datamaps.RegisterDataMapTypesEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import owmii.powah.api.PowahAPI;
import owmii.powah.api.FluidCoolantConfig;
import owmii.powah.api.MagmatorFuelValue;
import owmii.powah.api.PassiveHeatSourceConfig;
import owmii.powah.api.SolidCoolantConfig;
import owmii.powah.block.Blcks;
import owmii.powah.block.Tiles;
import owmii.powah.compat.curios.CuriosCompat;
Expand Down Expand Up @@ -79,17 +79,6 @@ public Powah(IEventBus modEventBus) {
modEventBus.addListener(Network::register);
modEventBus.addListener(this::registerDataTypeMaps);

modEventBus.addListener((FMLCommonSetupEvent event) -> {
// TODO: move to config
PowahAPI.registerCoolant(BuiltInRegistries.FLUID.getKey(Fluids.WATER), 1);
PowahAPI.registerSolidCoolant(Blocks.SNOW_BLOCK, 48, -3);
PowahAPI.registerSolidCoolant(Items.SNOWBALL, 12, -3);
PowahAPI.registerSolidCoolant(Blocks.ICE, 48, -5);
PowahAPI.registerSolidCoolant(Blocks.PACKED_ICE, 192, -8);
PowahAPI.registerSolidCoolant(Blocks.BLUE_ICE, 568, -17);
PowahAPI.registerSolidCoolant(Blcks.DRY_ICE.get(), 712, -32);
});

modEventBus.addListener(PowahDataGenerator::gatherData);
NeoForge.EVENT_BUS.addListener((PlayerInteractEvent.RightClickBlock event) -> {
if (event.getUseBlock() == TriState.FALSE) {
Expand All @@ -108,6 +97,11 @@ public Powah(IEventBus modEventBus) {

private void registerDataTypeMaps(RegisterDataMapTypesEvent event) {
event.register(ReactorFuel.DATA_MAP_TYPE);
event.register(SolidCoolantConfig.DATA_MAP_TYPE);
event.register(PassiveHeatSourceConfig.BLOCK_DATA_MAP);
event.register(PassiveHeatSourceConfig.FLUID_DATA_MAP);
event.register(FluidCoolantConfig.DATA_MAP_TYPE);
event.register(MagmatorFuelValue.DATA_MAP_TYPE);
}

private void registerTransfer(RegisterCapabilitiesEvent event) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/owmii/powah/api/FluidCoolantConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package owmii.powah.api;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.registries.datamaps.DataMapType;
import owmii.powah.Powah;

public record FluidCoolantConfig(int temperature) {
public static final Codec<FluidCoolantConfig> CODEC = RecordCodecBuilder.create(builder -> builder
.group(
Codec.INT.fieldOf("temperature").forGetter(FluidCoolantConfig::temperature))
.apply(builder, FluidCoolantConfig::new));
public static final DataMapType<Fluid, FluidCoolantConfig> DATA_MAP_TYPE = DataMapType.builder(Powah.id("fluid_coolant"), Registries.FLUID, CODEC)
.synced(CODEC, true)
.build();
}
24 changes: 24 additions & 0 deletions src/main/java/owmii/powah/api/MagmatorFuelValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package owmii.powah.api;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.registries.datamaps.DataMapType;
import owmii.powah.Powah;

/**
* Fuel for the {@link owmii.powah.block.magmator.MagmatorTile}.
*
* @param energyProduced FE per 100mb of fluid
*/
public record MagmatorFuelValue(int energyProduced) {
public static final Codec<MagmatorFuelValue> CODEC = RecordCodecBuilder.create(builder -> builder
.group(
Codec.INT.fieldOf("energy_produced").forGetter(MagmatorFuelValue::energyProduced))
.apply(builder, MagmatorFuelValue::new));

public static final DataMapType<Fluid, MagmatorFuelValue> DATA_MAP_TYPE = DataMapType.builder(Powah.id("magmator_fuel"), Registries.FLUID, CODEC)
.synced(CODEC, true)
.build();
}
24 changes: 24 additions & 0 deletions src/main/java/owmii/powah/api/PassiveHeatSourceConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package owmii.powah.api;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.registries.datamaps.DataMapType;
import owmii.powah.Powah;

public record PassiveHeatSourceConfig(int temperature) {
public static final Codec<PassiveHeatSourceConfig> CODEC = RecordCodecBuilder.create(builder -> builder
.group(
Codec.INT.fieldOf("temperature").forGetter(PassiveHeatSourceConfig::temperature))
.apply(builder, PassiveHeatSourceConfig::new));
public static final DataMapType<Block, PassiveHeatSourceConfig> BLOCK_DATA_MAP = DataMapType
.builder(Powah.id("heat_source"), Registries.BLOCK, CODEC)
.synced(CODEC, true)
.build();
public static final DataMapType<Fluid, PassiveHeatSourceConfig> FLUID_DATA_MAP = DataMapType
.builder(Powah.id("heat_source"), Registries.FLUID, CODEC)
.synced(CODEC, true)
.build();
}
81 changes: 24 additions & 57 deletions src/main/java/owmii/powah/api/PowahAPI.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
package owmii.powah.api;

import java.util.HashMap;
import java.util.Map;
import java.util.OptionalInt;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.material.Fluid;
import org.apache.commons.lang3.tuple.Pair;

public class PowahAPI {

// Magmatic gen fluids
public static final Map<ResourceLocation, Integer> MAGMATIC_FLUIDS = new HashMap<>();

// Coolants/heat sources
public static final Map<ResourceLocation, Integer> COOLANT_FLUIDS = new HashMap<>();
public static final Map<ResourceLocation, Pair<Integer, Integer>> SOLID_COOLANTS = new HashMap<>();
public static final Map<ResourceLocation, Integer> HEAT_SOURCES = new HashMap<>();

/**
* Register fluid that used to fuel the Magmatic generator.
*
* @param fluid: the fluid used as fuel.
* @param heat: the heat of the fluid.
**/
public static void registerMagmaticFluid(ResourceLocation fluid, int heat) {
MAGMATIC_FLUIDS.put(fluid, heat);
private PowahAPI() {
}

/**
Expand All @@ -36,17 +17,11 @@ public static void registerMagmaticFluid(ResourceLocation fluid, int heat) {
* @return the heat value;
**/
public static int getMagmaticFluidHeat(Fluid fluid) {
return MAGMATIC_FLUIDS.getOrDefault(BuiltInRegistries.FLUID.getKey(fluid), 0);
}

/**
* Register a coolant fluid.
*
* @param fluid: the fluid used as coolant.
* @param cooling: the coldness of the fluid.
**/
public static void registerCoolant(ResourceLocation fluid, int cooling) {
COOLANT_FLUIDS.put(fluid, cooling);
var config = BuiltInRegistries.FLUID.getData(PassiveHeatSourceConfig.FLUID_DATA_MAP, fluid.builtInRegistryHolder().key());
if (config == null) {
return 0;
}
return config.temperature();
}

/**
Expand All @@ -55,18 +30,12 @@ public static void registerCoolant(ResourceLocation fluid, int cooling) {
* @param fluid: the fluid used as coolant.
* @return the coldness value;
**/
public static int getCoolant(Fluid fluid) {
return COOLANT_FLUIDS.getOrDefault(BuiltInRegistries.FLUID.getKey(fluid), 0);
}

/**
* Register the heat source block/fluid block.
*
* @param block: the block used as heat source.
* @param heat: the heat of the block.
**/
public static void registerHeatSource(ResourceLocation block, int heat) {
HEAT_SOURCES.put(block, heat);
public static OptionalInt getCoolant(Fluid fluid) {
var config = BuiltInRegistries.FLUID.getData(FluidCoolantConfig.DATA_MAP_TYPE, fluid.builtInRegistryHolder().key());
if (config == null) {
return OptionalInt.empty();
}
return OptionalInt.of(config.temperature());
}

/**
Expand All @@ -76,17 +45,11 @@ public static void registerHeatSource(ResourceLocation block, int heat) {
* @return the heat of the block;
**/
public static int getHeatSource(Block block) {
return HEAT_SOURCES.getOrDefault(BuiltInRegistries.BLOCK.getKey(block), 0);
}

/**
* Register a solid coolant item.
*
* @param item: the stack used as solid coolant.
* @param cooling: the coldness of the stack.
**/
public static void registerSolidCoolant(ItemLike item, int size, int cooling) {
SOLID_COOLANTS.put(BuiltInRegistries.ITEM.getKey(item.asItem()), Pair.of(size, cooling));
var config = BuiltInRegistries.BLOCK.getData(PassiveHeatSourceConfig.BLOCK_DATA_MAP, block.builtInRegistryHolder().key());
if (config == null) {
return 0;
}
return config.temperature();
}

/**
Expand All @@ -95,7 +58,11 @@ public static void registerSolidCoolant(ItemLike item, int size, int cooling) {
* @param item: the stack used as solid coolant.
* @return the coldness value;
**/
public static Pair<Integer, Integer> getSolidCoolant(ItemLike item) {
return SOLID_COOLANTS.getOrDefault(BuiltInRegistries.ITEM.getKey(item.asItem()), Pair.of(0, 0));
public static SolidCoolantConfig getSolidCoolant(ItemLike item) {
var config = BuiltInRegistries.ITEM.getData(SolidCoolantConfig.DATA_MAP_TYPE, item.asItem().builtInRegistryHolder().key());
if (config == null) {
return new SolidCoolantConfig(0, 0);
}
return config;
}
}
19 changes: 19 additions & 0 deletions src/main/java/owmii/powah/api/SolidCoolantConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package owmii.powah.api;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.Item;
import net.neoforged.neoforge.registries.datamaps.DataMapType;
import owmii.powah.Powah;

public record SolidCoolantConfig(int amount, int temperature) {
public static final Codec<SolidCoolantConfig> CODEC = RecordCodecBuilder.create(builder -> builder
.group(
Codec.INT.fieldOf("amount").forGetter(SolidCoolantConfig::amount),
Codec.INT.fieldOf("temperature").forGetter(SolidCoolantConfig::temperature))
.apply(builder, SolidCoolantConfig::new));
public static final DataMapType<Item, SolidCoolantConfig> DATA_MAP_TYPE = DataMapType.builder(Powah.id("solid_coolant"), Registries.ITEM, CODEC)
.synced(CODEC, true)
.build();
}
15 changes: 7 additions & 8 deletions src/main/java/owmii/powah/block/reactor/ReactorTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.common.Tags;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;
import owmii.powah.api.PowahAPI;
import owmii.powah.block.Tier;
Expand Down Expand Up @@ -49,7 +48,7 @@ public class ReactorTile extends AbstractEnergyProvider<ReactorBlock> implements
public ReactorTile(BlockPos pos, BlockState state, Tier variant) {
super(Tiles.REACTOR.get(), pos, state, variant);
this.tank.setCapacity(Util.bucketAmount())
.setValidator(stack -> PowahAPI.getCoolant(stack.getFluid()) != 0)
.setValidator(stack -> PowahAPI.getCoolant(stack.getFluid()).isPresent())
.setChange(() -> ReactorTile.this.sync(10));
this.inv.add(5);
}
Expand Down Expand Up @@ -197,9 +196,9 @@ private boolean processTemperature(Level world, boolean generating) {
if (this.solidCoolant.isEmpty()) {
ItemStack stack = this.inv.getStackInSlot(4);
if (!stack.isEmpty()) {
Pair<Integer, Integer> coolant = PowahAPI.getSolidCoolant(stack.getItem());
int size = coolant.getLeft();
int temp = coolant.getRight();
var coolant = PowahAPI.getSolidCoolant(stack.getItem());
int size = coolant.amount();
int temp = coolant.temperature();
if (size > 0 && temp < 2) {
this.solidCoolant.setAll(size);
this.solidCoolantTemp = temp;
Expand All @@ -225,7 +224,7 @@ private boolean processTemperature(Level world, boolean generating) {
}
double temp = Math.min(this.baseTemp + this.carbonTemp + this.redstoneTemp, this.temp.getMax());
if (!this.tank.isEmpty()) {
int coldness = -PowahAPI.getCoolant(this.tank.getFluid().getFluid());
int coldness = -PowahAPI.getCoolant(this.tank.getFluid().getFluid()).orElse(0);
int i = Math.abs(coldness + this.solidCoolantTemp) + 1;
temp /= i;
sync(5);
Expand Down Expand Up @@ -349,8 +348,8 @@ public boolean canInsert(int slot, ItemStack stack) {
} else if (slot == 3) {
return stack.is(Tags.Items.DUSTS_REDSTONE) || stack.is(Tags.Items.STORAGE_BLOCKS_REDSTONE);
} else if (slot == 4) {
Pair<Integer, Integer> coolant = PowahAPI.getSolidCoolant(stack.getItem());
return coolant.getLeft() > 0 && coolant.getRight() < 2;
var coolant = PowahAPI.getSolidCoolant(stack.getItem());
return coolant.amount() > 0 && coolant.temperature() < 2;
} else
return Energy.chargeable(stack);
}
Expand Down
Loading

0 comments on commit a7acf98

Please sign in to comment.