Skip to content

Commit

Permalink
Made gases data-driven (crashes)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juniper Primavera committed Oct 9, 2023
1 parent 81e5c5f commit 9c53a91
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
9 changes: 5 additions & 4 deletions src/main/java/io/github/teampropulsive/Items.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import net.minecraft.registry.RegistryKeys;
import net.minecraft.text.Text;

import static io.github.teampropulsive.Propulsive.gasDictionary;
import static io.github.teampropulsive.util.Gases.*;

public class Items {
public static final Item OXYGEN_TANK = new GasCanister(new FabricItemSettings(), OXYGEN, 1000);
public static final Item OXYGEN_CANISTER = new GasCanister(new FabricItemSettings(), OXYGEN, 1000);
public static final Item METHANE_CANISTER = new GasCanister(new FabricItemSettings(), METHANE, 1000);
public static final Item HYDROGEN_CANISTER = new GasCanister(new FabricItemSettings(), HYDROGEN, 1000);
public static final Item OXYGEN_TANK = new GasCanister(new FabricItemSettings(), gasDictionary.get("gas/oxygen"), 1000);
public static final Item OXYGEN_CANISTER = new GasCanister(new FabricItemSettings(), gasDictionary.get("gas/oxygen"), 1000);
public static final Item METHANE_CANISTER = new GasCanister(new FabricItemSettings(), gasDictionary.get("gas/methane"), 1000);
public static final Item HYDROGEN_CANISTER = new GasCanister(new FabricItemSettings(), gasDictionary.get("gas/hydrogen"), 1000);
public static final Item ALUMINUM_NUGGET = new Item(new FabricItemSettings());
public static final Item RAW_BAUXITE = new Item(new FabricItemSettings());
public static final Item ALUMINUM_INGOT = new Item(new FabricItemSettings());
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/io/github/teampropulsive/Propulsive.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import io.github.teampropulsive.celestial.Star;
import io.github.teampropulsive.celestial.Terrestrial;
import io.github.teampropulsive.data.GasReloadListener;
import io.github.teampropulsive.data.PlanetReloadListener;
import io.github.teampropulsive.data.RocketReloadListener;
import io.github.teampropulsive.screen.BlueprintTableScreenHandler;
import io.github.teampropulsive.types.AtmoCompositionGas;
import io.github.teampropulsive.types.Gas;
import io.github.teampropulsive.types.Planet;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
Expand All @@ -19,8 +21,11 @@
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourceReloader;
import net.minecraft.resource.ResourceType;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
Expand All @@ -31,6 +36,8 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Set;

import static io.github.teampropulsive.util.Gases.*;

Expand Down Expand Up @@ -73,6 +80,7 @@ public void onInitialize() {
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new GasReloadListener());
}

public static Dictionary<String, Gas> gasDictionary;
// Dimensions
public static RegistryKey<World> SPACE = RegistryKey.of(RegistryKeys.WORLD, new Identifier("propulsive:space"));

Expand All @@ -86,9 +94,9 @@ public void onInitialize() {
Propulsive.id("textures/celestial/terrestrial/earth_icon.png"),
Propulsive.id("textures/celestial/terrestrial/earth.png"),
new AtmoCompositionGas[]{
new AtmoCompositionGas(OXYGEN, 1.0),
new AtmoCompositionGas(HYDROGEN, 0.01),
new AtmoCompositionGas(METHANE, 0.05)
new AtmoCompositionGas(gasDictionary.get("gas/oxygen"), 1.0),
new AtmoCompositionGas(gasDictionary.get("gas/hydrogen"), 0.01),
new AtmoCompositionGas(gasDictionary.get("gas/methane"), 0.05)
}
);

Expand Down
18 changes: 15 additions & 3 deletions src/main/java/io/github/teampropulsive/data/GasReloadListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

import com.google.gson.Gson;

import com.google.gson.JsonObject;
import io.github.teampropulsive.Items;
import io.github.teampropulsive.Propulsive;
import io.github.teampropulsive.types.Gas;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.predicate.BlockPredicate;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import static io.github.teampropulsive.Propulsive.gasDictionary;
import static io.github.teampropulsive.Propulsive.id;

public class GasReloadListener implements SimpleSynchronousResourceReloadListener {
@Override
public Identifier getFabricId() {
return Propulsive.id("gas");
return id("gas");
}

@Override
Expand All @@ -28,8 +34,14 @@ public void reload(ResourceManager manager) {
Identifier id = entry.getKey();
Resource resource = entry.getValue();
try (InputStream stream = resource.getInputStream()) { // Pretty sure this works, just gotta register it somehow (?)
Gas new_gas = new Gson().fromJson(new String(stream.readAllBytes(), StandardCharsets.UTF_8), Gas.class);

String contents = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
JsonObject object = JsonHelper.deserialize(contents);
JsonObject densityJsonKey = object.getAsJsonObject("density");
double density = densityJsonKey.getAsDouble();
JsonObject identifierJsonKey = object.getAsJsonObject("density");
String identifier = identifierJsonKey.getAsString();
Gas gas = new Gas(id(identifier), density);
gasDictionary.put(identifier, gas);
} catch (Exception e) {
Propulsive.LOGGER.error("Error occurred while loading gas " + id.toString(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void reload(ResourceManager manager) {
JsonObject object = JsonHelper.deserialize(contents);
JsonObject jsonKey = object.getAsJsonObject("key");
Map<String, BlockPredicate> key = parseKey(jsonKey);

Propulsive.LOGGER.info(key.toString());

Propulsive.LOGGER.info("Pretend like this is doing something useful with " + object.toString());
} catch (Exception e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/teampropulsive/util/Gases.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.teampropulsive.types.Gas;

public class Gases {
public static final Gas OXYGEN = new Gas(Propulsive.id("gas/oxygen"), 1.429);
//public static final Gas OXYGEN = new Gas(Propulsive.id("gas/oxygen"), 1.429);
public static final Gas METHANE = new Gas(Propulsive.id("gas/methane"), 0.657);
public static final Gas HYDROGEN = new Gas(Propulsive.id("gas/hydrogen"), 0.08988);
}
2 changes: 1 addition & 1 deletion src/main/resources/data/propulsive/gases/oxygen.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"density": 1.429,
"identifier": "propulsive:gas/oxygen"
"identifier": "gas/oxygen"
}

0 comments on commit 9c53a91

Please sign in to comment.