Skip to content

Commit

Permalink
1.1.2 Update
Browse files Browse the repository at this point in the history
Fixed items with tags merging
Added terminal sorting modes
Improved terminal gui handler
  • Loading branch information
tom5454 committed May 30, 2020
1 parent ff40deb commit 89d86ac
Show file tree
Hide file tree
Showing 13 changed files with 536 additions and 67 deletions.
76 changes: 73 additions & 3 deletions src/main/java/com/tom/storagemod/StoredItemStack.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tom.storagemod;

import java.util.Comparator;
import java.util.function.Function;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
Expand Down Expand Up @@ -46,7 +47,7 @@ public static StoredItemStack readFromNBT(CompoundNBT tag) {
return !stack.stack.isEmpty() ? stack : null;
}

public static class ComparatorAmount implements Comparator<StoredItemStack> {
public static class ComparatorAmount implements IStoredItemStackComparator {
public boolean reversed;

public ComparatorAmount(boolean reversed) {
Expand All @@ -58,6 +59,71 @@ public int compare(StoredItemStack in1, StoredItemStack in2) {
int c = in2.getQuantity() > in1.getQuantity() ? 1 : (in1.getQuantity() == in2.getQuantity() ? in1.getStack().getDisplayName().getUnformattedComponentText().compareTo(in2.getStack().getDisplayName().getUnformattedComponentText()) : -1);
return this.reversed ? -c : c;
}

@Override
public boolean isReversed() {
return reversed;
}

@Override
public int type() {
return 0;
}

@Override
public void setReversed(boolean rev) {
reversed = rev;
}
}

public static class ComparatorName implements IStoredItemStackComparator {
public boolean reversed;

public ComparatorName(boolean reversed) {
this.reversed = reversed;
}

@Override
public int compare(StoredItemStack in1, StoredItemStack in2) {
int c = in1.getDisplayName().compareTo(in2.getDisplayName());
return this.reversed ? -c : c;
}

@Override
public boolean isReversed() {
return reversed;
}

@Override
public int type() {
return 1;
}

@Override
public void setReversed(boolean rev) {
reversed = rev;
}
}

public static interface IStoredItemStackComparator extends Comparator<StoredItemStack> {
boolean isReversed();
void setReversed(boolean rev);
int type();
}

public static enum SortingTypes {
AMOUNT(ComparatorAmount::new),
NAME(ComparatorName::new)
;
public static final SortingTypes[] VALUES = values();
private final Function<Boolean, IStoredItemStackComparator> factory;
private SortingTypes(Function<Boolean, IStoredItemStackComparator> factory) {
this.factory = factory;
}

public IStoredItemStackComparator create(boolean rev) {
return factory.apply(rev);
}
}

@Override
Expand All @@ -70,6 +136,10 @@ public int hashCode() {
return result;
}

public String getDisplayName() {
return stack.getDisplayName().getUnformattedComponentText();
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
Expand All @@ -79,7 +149,7 @@ public boolean equals(Object obj) {
//if (count != other.count) return false;
if (stack == null) {
if (other.stack != null) return false;
} else if (!ItemStack.areItemsEqual(stack, other.stack)) return false;
} else if (!ItemStack.areItemsEqual(stack, other.stack) || !ItemStack.areItemStackTagsEqual(stack, other.stack)) return false;
return true;
}

Expand All @@ -89,7 +159,7 @@ public boolean equals(StoredItemStack other) {
if (count != other.count) return false;
if (stack == null) {
if (other.stack != null) return false;
} else if (!ItemStack.areItemsEqual(stack, other.stack)) return false;
} else if (!ItemStack.areItemsEqual(stack, other.stack) || !ItemStack.areItemStackTagsEqual(stack, other.stack)) return false;
return true;
}

Expand Down
43 changes: 19 additions & 24 deletions src/main/java/com/tom/storagemod/gui/ContainerCraftingTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.tom.storagemod.StoredItemStack;
import com.tom.storagemod.jei.IJEIAutoFillTerminal;
import com.tom.storagemod.network.IDataReceiver;
import com.tom.storagemod.network.NetworkHandler;
import com.tom.storagemod.tile.TileEntityCraftingTerminal;

public class ContainerCraftingTerminal extends ContainerStorageTerminal implements IJEIAutoFillTerminal, IDataReceiver {
Expand All @@ -54,7 +53,6 @@ public SlotCrafting(IInventory inventoryIn, int index, int xPosition, int yPosit
}
}

private final PlayerEntity player;
private final CraftingInventory craftMatrix;
private final CraftResultInventory craftResult;
private Slot craftingResultSlot;
Expand All @@ -74,7 +72,6 @@ public void removeListener(IContainerListener listener) {

public ContainerCraftingTerminal(int id, PlayerInventory inv, TileEntityCraftingTerminal te) {
super(StorageMod.craftingTerminalCont, id, inv, te);
this.player = inv.player;
craftMatrix = te.getCraftingInv();
craftResult = te.getCraftResult();
init();
Expand All @@ -84,7 +81,6 @@ public ContainerCraftingTerminal(int id, PlayerInventory inv, TileEntityCrafting

public ContainerCraftingTerminal(int id, PlayerInventory inv) {
super(StorageMod.craftingTerminalCont, id, inv);
this.player = inv.player;
craftMatrix = new CraftingInventory(this, 3, 3);
craftResult = new CraftResultInventory();
init();
Expand All @@ -101,13 +97,13 @@ public void onContainerClosed(PlayerEntity playerIn) {
private void init() {
int x = -4;
int y = 94;
this.addSlot(craftingResultSlot = new CraftingResultSlot(player, craftMatrix, craftResult, 0, x + 124, y + 35) {
this.addSlot(craftingResultSlot = new CraftingResultSlot(pinv.player, craftMatrix, craftResult, 0, x + 124, y + 35) {
@Override
public ItemStack onTake(PlayerEntity thePlayer, ItemStack stack) {
if (thePlayer.world.isRemote)
return ItemStack.EMPTY;
this.onCrafting(stack);
if (!player.getEntityWorld().isRemote) {
if (!pinv.player.getEntityWorld().isRemote) {
((TileEntityCraftingTerminal) te).craft(thePlayer);
}
return ItemStack.EMPTY;
Expand Down Expand Up @@ -174,8 +170,9 @@ public void onCraftMatrixChanged() {

@Override
public boolean enchantItem(PlayerEntity playerIn, int id) {
if(te != null)
if(te != null && id == 0)
((TileEntityCraftingTerminal) te).clear();
else super.enchantItem(playerIn, id);
return false;
}

Expand All @@ -192,7 +189,7 @@ public void clear() {

@Override
public boolean matches(IRecipe<? super CraftingInventory> recipeIn) {
return recipeIn.matches(this.craftMatrix, this.player.world);
return recipeIn.matches(this.craftMatrix, this.pinv.player.world);
}

@Override
Expand Down Expand Up @@ -305,25 +302,23 @@ protected void clear() {
}).place(p_217056_3_, p_217056_2_, p_217056_1_);
}

@Override
public void sendMessage(CompoundNBT compound) {
NetworkHandler.sendDataToServer(compound);
}

@Override
public void receive(CompoundNBT message) {
ItemStack[][] stacks = new ItemStack[9][];
ListNBT list = message.getList("i", 10);
for (int i = 0;i < list.size();i++) {
CompoundNBT nbttagcompound = list.getCompound(i);
byte slot = nbttagcompound.getByte("s");
byte l = nbttagcompound.getByte("l");
stacks[slot] = new ItemStack[l];
for (int j = 0;j < l;j++) {
CompoundNBT tag = nbttagcompound.getCompound("i" + j);
stacks[slot][j] = ItemStack.read(tag);
super.receive(message);
if(message.contains("i")) {
ItemStack[][] stacks = new ItemStack[9][];
ListNBT list = message.getList("i", 10);
for (int i = 0;i < list.size();i++) {
CompoundNBT nbttagcompound = list.getCompound(i);
byte slot = nbttagcompound.getByte("s");
byte l = nbttagcompound.getByte("l");
stacks[slot] = new ItemStack[l];
for (int j = 0;j < l;j++) {
CompoundNBT tag = nbttagcompound.getCompound("i" + j);
stacks[slot][j] = ItemStack.read(tag);
}
}
((TileEntityCraftingTerminal) te).handlerItemTransfer(pinv.player, stacks);
}
((TileEntityCraftingTerminal) te).handlerItemTransfer(player, stacks);
}
}
Loading

0 comments on commit 89d86ac

Please sign in to comment.