Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/silvermoon/boxplusplus/boxplusplus.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void postInit(FMLPostInitializationEvent event) {
}

@Mod.EventHandler
public void serverAboutToStart(FMLServerAboutToStartEvent event) {
proxy.serverAboutToStart(event);
public void serverStarted(FMLServerStartedEvent event) {
proxy.serverStarted(event);
}

public static final CreativeTabs BoxTab = new CreativeTabs("BoxPlusPlus") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void init(FMLInitializationEvent event) {
// postInit "Handle interaction with other mods, complete your setup based on this." (Remove if not needed)
public void postInit(FMLPostInitializationEvent event) {}

public void serverAboutToStart(FMLServerAboutToStartEvent event) {
public void serverStarted(FMLServerStartedEvent event) {
new RecipeLoader().run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static void addModuleRecipe() {
new ItemStack(BlockRegister.BoxRing2, 2),
GTModHandler.getModItem(GregTech.ID, "gt.blockmachines", 64, 31077),
GTModHandler.getModItem(GregTech.ID, "gt.blockmachines", 64, 31065),
GTModHandler.getModItem(Avaritia.ID, "Neutronium_Compressor", 4),
GTModHandler.getModItem(GregTech.ID, "gt.blockmachines", 4, 3007),
GTModHandler.getModItem(GTPlusPlus.ID, "MU-metaitem.01", 16, 32044),
GTModHandler.getModItem(ExtraUtilities.ID, "nodeUpgrade", 64, 2),
GTModHandler.getModItem(GTPlusPlus.ID, "dummyResearch", 1),
Expand Down
66 changes: 53 additions & 13 deletions src/main/java/com/silvermoon/boxplusplus/util/FluidContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,75 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;

public class FluidContainer {

private final HashMap<String, FluidStack> stack = new HashMap<>();
private final Map<FluidKey, FluidStack> stacks = new HashMap<>();

public void addFluidStack(FluidStack input, int multiple) {
String name = input.getUnlocalizedName();
if (!stack.containsKey(name)) {
stack.put(
name,
new FluidStack(input.getFluid(), (int) Math.min((long) input.amount * multiple, Integer.MAX_VALUE)));
if (input == null || input.getFluid() == null) return;

NBTTagCompound nbt = input.tag != null ? (NBTTagCompound) input.tag.copy() : null;

FluidKey key = new FluidKey(input.getFluid(), nbt);
int amountToAdd = input.amount * multiple;

if (stacks.containsKey(key)) {
FluidStack stack = stacks.get(key);
stack.amount = (int) Math.min((long) stack.amount * amountToAdd, Integer.MAX_VALUE);
} else {
stack.get(name).amount = (int) Math
.min(stack.get(name).amount + (long) input.amount * multiple, Integer.MAX_VALUE);
FluidStack copied = new FluidStack(input, amountToAdd);
copied.tag = nbt;
stacks.put(key, copied);
}
}

public FluidContainer addFluidStackList(List<FluidStack> list, int multiple) {
for (FluidStack var1 : list) {
addFluidStack(var1, multiple);
for (FluidStack fluidStack : list) {
if (fluidStack != null) addFluidStack(fluidStack, multiple);
}
return this;
}

public List<FluidStack> getFluidStack() {
List<FluidStack> output = new ArrayList<>();
stack.forEach((k, v) -> output.add(v));
return output;
return new ArrayList<>(stacks.values());
}

private static class FluidKey {

private final String fluidName;
private final NBTTagCompound nbt;
private final int hashCode;

public FluidKey(Fluid fluid, NBTTagCompound nbt) {
this.fluidName = fluid.getName();
this.nbt = nbt != null ? (NBTTagCompound) nbt.copy() : null;

this.hashCode = 31 * fluidName.hashCode() + (nbt != null ? nbt.hashCode() : 0);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
FluidKey other = (FluidKey) obj;
return fluidName.equals(other.fluidName) && nbtEquals(nbt, other.nbt);
}

@Override
public int hashCode() {
return hashCode;
}

private boolean nbtEquals(NBTTagCompound nbt1, NBTTagCompound nbt2) {
if (nbt1 == nbt2) return true;
if (nbt1 == null || nbt2 == null) return false;
return nbt1.equals(nbt2);
}
}
}
106 changes: 77 additions & 29 deletions src/main/java/com/silvermoon/boxplusplus/util/ItemContainer.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,105 @@
package com.silvermoon.boxplusplus.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import net.minecraft.nbt.NBTTagCompound;

public class ItemContainer {

private final Table<Item, Integer, Long> stack = HashBasedTable.create();
private final Map<ItemStackKey, Long> stack = new HashMap<>();

public void addItemStack(ItemStack input, int multiple, int chance) {
if (input.getItem() == null) return;
Optional<Long> isNull = Optional.ofNullable(stack.get(input.getItem(), input.getItemDamage()));
if (isNull.isPresent()) {
stack
.put(input.getItem(), input.getItemDamage(), chance * (long) input.stackSize * multiple + isNull.get());
} else {
stack.put(input.getItem(), input.getItemDamage(), chance * (long) input.stackSize * multiple);
}
if (input == null || input.getItem() == null) return;

NBTTagCompound nbt = input.getTagCompound() != null ? (NBTTagCompound) input.getTagCompound()
.copy() : null;

ItemStackKey key = new ItemStackKey(input.getItem(), input.getItemDamage(), nbt);
long increment = (long) input.stackSize * multiple * chance;

stack.put(key, stack.getOrDefault(key, 0L) + increment);
}

public ItemContainer addItemStackList(List<ItemStack> list, int multiple) {
for (ItemStack var1 : list) {
if (var1 == null) continue;
addItemStack(var1, multiple, 10000);
for (ItemStack stack : list) {
if (stack == null) continue;
addItemStack(stack, multiple, 10000);
}
return this;
}

public void addItemStackList(List<ItemStack> list, List<Integer> chance, int multiple) {
public void addItemStackList(List<ItemStack> list, List<Integer> chances, int multiple) {
for (int i = 0; i < list.size(); i++) {
addItemStack(list.get(i), multiple, chance.get(i));
ItemStack stack = list.get(i);
if (stack != null) {
int chance = (i < chances.size()) ? chances.get(i) : 10000;
addItemStack(stack, multiple, chance);
}
}
}

public List<ItemStack> getItemStack() {
List<ItemStack> output = new ArrayList<>();
for (Item item : stack.rowKeySet()) {
for (int meta : stack.columnKeySet()) {
if (stack.get(item, meta) != null) {
output.add(
new ItemStack(
item,
(int) Math.min(stack.get(item, meta) / 10000, Integer.MAX_VALUE - 1),
meta));
}
for (Map.Entry<ItemStackKey, Long> entry : stack.entrySet()) {
ItemStackKey key = entry.getKey();
long total = entry.getValue();
int count = (int) Math.min(total / 10000, Integer.MAX_VALUE - 1);

ItemStack stack = new ItemStack(key.getItem(), count, key.getMeta());
if (key.getNbt() != null) {
stack.setTagCompound(
(NBTTagCompound) key.getNbt()
.copy());
}
output.add(stack);
}
return output;
}

private static class ItemStackKey {

private final Item item;
private final int meta;
private final NBTTagCompound nbt;

public ItemStackKey(Item item, int meta, NBTTagCompound nbt) {
this.item = item;
this.meta = meta;
this.nbt = nbt;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
ItemStackKey other = (ItemStackKey) obj;
return meta == other.meta && item.equals(other.item) && nbtEquals(nbt, other.nbt);
}

@Override
public int hashCode() {
int hash = item.hashCode() ^ meta;
return nbt != null ? hash ^ nbt.hashCode() : hash;
}

private boolean nbtEquals(NBTTagCompound nbt1, NBTTagCompound nbt2) {
if (nbt1 == nbt2) return true;
if (nbt1 == null || nbt2 == null) return false;
return nbt1.equals(nbt2);
}

public Item getItem() {
return item;
}

public int getMeta() {
return meta;
}

public NBTTagCompound getNbt() {
return nbt;
}
}
}