Skip to content

Commit

Permalink
refactor: hashed map -> string-indexed map
Browse files Browse the repository at this point in the history
  • Loading branch information
bruberu committed Aug 7, 2024
1 parent be22a9d commit 7715045
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import net.minecraft.item.ItemStack;

import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.NotNull;
Expand All @@ -15,14 +14,14 @@

public class FissionFuelRegistry {

private static final Map<Integer, IFissionFuelStats> HASHED_FUELS = new Int2ObjectArrayMap<>();
private static final Map<String, IFissionFuelStats> IDENTIFIED_FUELS = new Object2ObjectOpenHashMap<>();
private static final Map<ItemStack, IFissionFuelStats> FUELS = new Object2ObjectOpenCustomHashMap<>(
ItemStackHashStrategy.comparingAllButCount());
private static final Map<IFissionFuelStats, ItemStack> DEPLETED_FUELS = new Object2ObjectOpenHashMap<>();

public static void registerFuel(@NotNull ItemStack item, @NotNull IFissionFuelStats fuel,
@NotNull ItemStack depletedFuel) {
HASHED_FUELS.put(fuel.hashCode(), fuel);
IDENTIFIED_FUELS.put(fuel.getID(), fuel);
FUELS.put(item, fuel);
DEPLETED_FUELS.put(fuel, depletedFuel);
}
Expand All @@ -38,8 +37,8 @@ public static Collection<ItemStack> getAllFissionableRods() {
}

@Nullable
public static IFissionFuelStats getFissionFuel(int hash) {
return HASHED_FUELS.get(hash);
public static IFissionFuelStats getFissionFuel(String name) {
return IDENTIFIED_FUELS.get(name);
}

public static @NotNull ItemStack getDepletedFuel(IFissionFuelStats stats) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ default int getNeutronGenerationTimeCategory() {
return 3;
}
}

/**
* @return A unique ID for this fuel.
*/
String getID();
}
129 changes: 45 additions & 84 deletions src/main/java/gregtech/api/unification/material/Material.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,7 @@ public Builder(int id, @NotNull ResourceLocation resourceLocation) {
*/

/**
* Add a {@link FluidProperty} to this Material.<br>
* Will be created as a {@link FluidStorageKeys#LIQUID}, with
* Add a {@link FluidProperty} to this Material.<br> Will be created as a {@link FluidStorageKeys#LIQUID}, with
* standard {@link FluidBuilder} defaults.
* <p>
* Can be called multiple times to add multiple fluids.
Expand All @@ -506,8 +505,7 @@ public Builder fluid() {
}

/**
* Add a {@link FluidProperty} to this Material.<br>
* Will be created with the specified state a with standard
* Add a {@link FluidProperty} to this Material.<br> Will be created with the specified state a with standard
* {@link FluidBuilder} defaults.
* <p>
* Can be called multiple times to add multiple fluids.
Expand Down Expand Up @@ -600,8 +598,7 @@ public Builder gas(@NotNull FluidBuilder builder) {
}

/**
* Add a {@link DustProperty} to this Material.<br>
* Will be created with a Harvest Level of 2 and no Burn Time
* Add a {@link DustProperty} to this Material.<br> Will be created with a Harvest Level of 2 and no Burn Time
* (Furnace Fuel).
*
* @throws IllegalArgumentException If a {@link DustProperty} has already been added to this Material.
Expand All @@ -612,11 +609,9 @@ public Builder dust() {
}

/**
* Add a {@link DustProperty} to this Material.<br>
* Will be created with no Burn Time (Furnace Fuel).
* Add a {@link DustProperty} to this Material.<br> Will be created with no Burn Time (Furnace Fuel).
*
* @param harvestLevel The Harvest Level of this block for Mining.<br>
* If this Material also has a
* @param harvestLevel The Harvest Level of this block for Mining.<br> If this Material also has a
* {@link ToolProperty}, this value will also be used to determine the tool's Mining Level.
* @throws IllegalArgumentException If a {@link DustProperty} has already been added to this Material.
*/
Expand All @@ -627,8 +622,7 @@ public Builder dust(int harvestLevel) {
/**
* Add a {@link DustProperty} to this Material.
*
* @param harvestLevel The Harvest Level of this block for Mining.<br>
* If this Material also has a
* @param harvestLevel The Harvest Level of this block for Mining.<br> If this Material also has a
* {@link ToolProperty}, this value will also be used to determine the tool's Mining Level.
* @param burnTime The Burn Time (in ticks) of this Material as a Furnace Fuel.
* @throws IllegalArgumentException If a {@link DustProperty} has already been added to this Material.
Expand All @@ -639,20 +633,17 @@ public Builder dust(int harvestLevel, int burnTime) {
}

/**
* Add a {@link WoodProperty} to this Material.<br>
* Will be created with a Harvest Level of 0 and a Burn Time of
* Add a {@link WoodProperty} to this Material.<br> Will be created with a Harvest Level of 0 and a Burn Time of
* 300 (Furnace Fuel).
*/
public Builder wood() {
return wood(0, 300);
}

/**
* Add a {@link WoodProperty} to this Material.<br>
* Will be created with a Burn Time of 300 (Furnace Fuel).
* Add a {@link WoodProperty} to this Material.<br> Will be created with a Burn Time of 300 (Furnace Fuel).
*
* @param harvestLevel The Harvest Level of this block for Mining.<br>
* If this Material also has a
* @param harvestLevel The Harvest Level of this block for Mining.<br> If this Material also has a
* {@link ToolProperty}, this value will also be used to determine the tool's Mining Level.
*/
public Builder wood(int harvestLevel) {
Expand All @@ -662,8 +653,7 @@ public Builder wood(int harvestLevel) {
/**
* Add a {@link WoodProperty} to this Material.
*
* @param harvestLevel The Harvest Level of this block for Mining.<br>
* If this Material also has a
* @param harvestLevel The Harvest Level of this block for Mining.<br> If this Material also has a
* {@link ToolProperty}, this value will also be used to determine the tool's Mining Level.
* @param burnTime The Burn Time (in ticks) of this Material as a Furnace Fuel.
*/
Expand All @@ -674,10 +664,8 @@ public Builder wood(int harvestLevel, int burnTime) {
}

/**
* Add an {@link IngotProperty} to this Material.<br>
* Will be created with a Harvest Level of 2 and no Burn Time
* (Furnace Fuel).<br>
* Will automatically add a {@link DustProperty} to this Material if it does not already
* Add an {@link IngotProperty} to this Material.<br> Will be created with a Harvest Level of 2 and no Burn Time
* (Furnace Fuel).<br> Will automatically add a {@link DustProperty} to this Material if it does not already
* have one.
*
* @throws IllegalArgumentException If an {@link IngotProperty} has already been added to this Material.
Expand All @@ -688,16 +676,12 @@ public Builder ingot() {
}

/**
* Add an {@link IngotProperty} to this Material.<br>
* Will be created with no Burn Time (Furnace Fuel).<br>
* Will
* Add an {@link IngotProperty} to this Material.<br> Will be created with no Burn Time (Furnace Fuel).<br> Will
* automatically add a {@link DustProperty} to this Material if it does not already have one.
*
* @param harvestLevel The Harvest Level of this block for Mining. 2 will make it require a iron tool.<br>
* If
* @param harvestLevel The Harvest Level of this block for Mining. 2 will make it require a iron tool.<br> If
* this Material also has a {@link ToolProperty}, this value will also be used to determine
* the tool's Mining level (-1). So 2 will make the tool harvest diamonds.<br>
* If this
* the tool's Mining level (-1). So 2 will make the tool harvest diamonds.<br> If this
* Material already had a Harvest Level defined, it will be overridden.
* @throws IllegalArgumentException If an {@link IngotProperty} has already been added to this Material.
*/
Expand All @@ -706,18 +690,14 @@ public Builder ingot(int harvestLevel) {
}

/**
* Add an {@link IngotProperty} to this Material.<br>
* Will automatically add a {@link DustProperty} to this
* Add an {@link IngotProperty} to this Material.<br> Will automatically add a {@link DustProperty} to this
* Material if it does not already have one.
*
* @param harvestLevel The Harvest Level of this block for Mining. 2 will make it require a iron tool.<br>
* If
* @param harvestLevel The Harvest Level of this block for Mining. 2 will make it require a iron tool.<br> If
* this Material also has a {@link ToolProperty}, this value will also be used to determine
* the tool's Mining level (-1). So 2 will make the tool harvest diamonds.<br>
* If this
* the tool's Mining level (-1). So 2 will make the tool harvest diamonds.<br> If this
* Material already had a Harvest Level defined, it will be overridden.
* @param burnTime The Burn Time (in ticks) of this Material as a Furnace Fuel.<br>
* If this Material already
* @param burnTime The Burn Time (in ticks) of this Material as a Furnace Fuel.<br> If this Material already
* had a Burn Time defined, it will be overridden.
* @throws IllegalArgumentException If an {@link IngotProperty} has already been added to this Material.
*/
Expand All @@ -733,10 +713,8 @@ public Builder ingot(int harvestLevel, int burnTime) {
}

/**
* Add a {@link GemProperty} to this Material.<br>
* Will be created with a Harvest Level of 2 and no Burn Time
* (Furnace Fuel).<br>
* Will automatically add a {@link DustProperty} to this Material if it does not already
* Add a {@link GemProperty} to this Material.<br> Will be created with a Harvest Level of 2 and no Burn Time
* (Furnace Fuel).<br> Will automatically add a {@link DustProperty} to this Material if it does not already
* have one.
*
* @throws IllegalArgumentException If a {@link GemProperty} has already been added to this Material.
Expand All @@ -747,34 +725,26 @@ public Builder gem() {
}

/**
* Add a {@link GemProperty} to this Material.<br>
* Will be created with no Burn Time (Furnace Fuel).<br>
* Will
* Add a {@link GemProperty} to this Material.<br> Will be created with no Burn Time (Furnace Fuel).<br> Will
* automatically add a {@link DustProperty} to this Material if it does not already have one.
*
* @param harvestLevel The Harvest Level of this block for Mining.<br>
* If this Material also has a
* @param harvestLevel The Harvest Level of this block for Mining.<br> If this Material also has a
* {@link ToolProperty}, this value will also be used to determine the tool's Mining
* level.<br>
* If this Material already had a Harvest Level defined, it will be overridden.
* level.<br> If this Material already had a Harvest Level defined, it will be overridden.
* @throws IllegalArgumentException If a {@link GemProperty} has already been added to this Material.
*/
public Builder gem(int harvestLevel) {
return gem(harvestLevel, 0);
}

/**
* Add a {@link GemProperty} to this Material.<br>
* Will automatically add a {@link DustProperty} to this
* Add a {@link GemProperty} to this Material.<br> Will automatically add a {@link DustProperty} to this
* Material if it does not already have one.
*
* @param harvestLevel The Harvest Level of this block for Mining.<br>
* If this Material also has a
* @param harvestLevel The Harvest Level of this block for Mining.<br> If this Material also has a
* {@link ToolProperty}, this value will also be used to determine the tool's Mining
* level.<br>
* If this Material already had a Harvest Level defined, it will be overridden.
* @param burnTime The Burn Time (in ticks) of this Material as a Furnace Fuel.<br>
* If this Material already
* level.<br> If this Material already had a Harvest Level defined, it will be overridden.
* @param burnTime The Burn Time (in ticks) of this Material as a Furnace Fuel.<br> If this Material already
* had a Burn Time defined, it will be overridden.
*/
public Builder gem(int harvestLevel, int burnTime) {
Expand All @@ -789,10 +759,8 @@ public Builder gem(int harvestLevel, int burnTime) {
}

/**
* Add a {@link PolymerProperty} to this Material.<br>
* Will be created with a Harvest Level of 2 and no Burn
* Time (Furnace Fuel).<br>
* Will automatically add a {@link DustProperty} to this Material if it does not
* Add a {@link PolymerProperty} to this Material.<br> Will be created with a Harvest Level of 2 and no Burn
* Time (Furnace Fuel).<br> Will automatically add a {@link DustProperty} to this Material if it does not
* already have one.
*
* @throws IllegalArgumentException If an {@link PolymerProperty} has already been added to this Material.
Expand All @@ -803,15 +771,12 @@ public Builder polymer() {
}

/**
* Add a {@link PolymerProperty} to this Material.<br>
* Will automatically add a {@link DustProperty} to this
* Add a {@link PolymerProperty} to this Material.<br> Will automatically add a {@link DustProperty} to this
* Material if it does not already have one. Will have a burn time of 0
*
* @param harvestLevel The Harvest Level of this block for Mining.<br>
* If this Material also has a
* @param harvestLevel The Harvest Level of this block for Mining.<br> If this Material also has a
* {@link ToolProperty}, this value will also be used to determine the tool's Mining
* level.<br>
* If this Material already had a Harvest Level defined, it will be overridden.
* level.<br> If this Material already had a Harvest Level defined, it will be overridden.
* @throws IllegalArgumentException If an {@link PolymerProperty} has already been added to this Material.
*/
public Builder polymer(int harvestLevel) {
Expand All @@ -833,8 +798,7 @@ public Builder burnTime(int burnTime) {
}

/**
* Set the Color of this Material.<br>
* Defaults to 0xFFFFFF unless {@link Builder#colorAverage()} was called,
* Set the Color of this Material.<br> Defaults to 0xFFFFFF unless {@link Builder#colorAverage()} was called,
* where it will be a weighted average of the components of the Material.
*
* @param color The RGB-formatted Color.
Expand All @@ -850,8 +814,7 @@ public Builder colorAverage() {
}

/**
* Set the {@link MaterialIconSet} of this Material.<br>
* Defaults vary depending on if the Material has a:<br>
* Set the {@link MaterialIconSet} of this Material.<br> Defaults vary depending on if the Material has a:<br>
* <ul>
* <li>{@link GemProperty}, it will default to {@link MaterialIconSet#GEM_VERTICAL}
* <li>{@link IngotProperty} or {@link DustProperty}, it will default to {@link MaterialIconSet#DULL}
Expand Down Expand Up @@ -890,8 +853,7 @@ public Builder components(ImmutableList<MaterialStack> components) {
}

/**
* Add {@link MaterialFlags} to this Material.<br>
* Dependent Flags (for example,
* Add {@link MaterialFlags} to this Material.<br> Dependent Flags (for example,
* {@link MaterialFlags#GENERATE_LONG_ROD} requiring {@link MaterialFlags#GENERATE_ROD}) will be automatically
* applied.
*/
Expand All @@ -901,8 +863,7 @@ public Builder flags(MaterialFlag... flags) {
}

/**
* Add {@link MaterialFlags} to this Material.<br>
* Dependent Flags (for example,
* Add {@link MaterialFlags} to this Material.<br> Dependent Flags (for example,
* {@link MaterialFlags#GENERATE_LONG_ROD} requiring {@link MaterialFlags#GENERATE_ROD}) will be automatically
* applied.
*
Expand Down Expand Up @@ -1094,7 +1055,8 @@ public Builder fissionFuel(int maxTemperature, int duration, double slowNeutronC
properties.setProperty(PropertyKey.FISSION_FUEL,
new FissionFuelProperty(maxTemperature, duration, slowNeutronCaptureCrossSection,
fastNeutronCaptureCrossSection, slowNeutronFissionCrossSection,
fastNeutronFissionCrossSection, neutronGenerationTime));
fastNeutronFissionCrossSection, neutronGenerationTime,
this.materialInfo.resourceLocation.toString()));
return this;
}

Expand Down Expand Up @@ -1181,13 +1143,12 @@ private void verifyInfo(MaterialProperties p, boolean averageRGB) {
iconSet = MaterialIconSet.GEM_VERTICAL;
} else if (p.hasProperty(PropertyKey.DUST) || p.hasProperty(PropertyKey.INGOT) ||
p.hasProperty(PropertyKey.POLYMER)) {
iconSet = MaterialIconSet.DULL;
} else
if (p.hasProperty(PropertyKey.FLUID)) {
iconSet = MaterialIconSet.FLUID;
} else {
iconSet = MaterialIconSet.DULL;
}
iconSet = MaterialIconSet.DULL;
} else if (p.hasProperty(PropertyKey.FLUID)) {
iconSet = MaterialIconSet.FLUID;
} else {
iconSet = MaterialIconSet.DULL;
}
}

// Verify MaterialRGB
Expand Down
Loading

0 comments on commit 7715045

Please sign in to comment.