Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First try at Refill #7

Merged
merged 13 commits into from
Dec 21, 2024
5 changes: 3 additions & 2 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ dependencies {
compileOnly("com.github.GTNewHorizons:EnderIO:2.8.22:dev")
compileOnly("com.github.GTNewHorizons:ironchest:6.0.87:dev")
compileOnly("com.github.GTNewHorizons:Avaritiaddons:1.8.4-GTNH:dev")
compileOnly("com.github.GTNewHorizons:CookingForBlockheads:1.3.7-GTNH:dev")
compileOnly("com.github.GTNewHorizons:CookingForBlockheads:1.3.8-GTNH:dev")
compileOnly("com.github.GTNewHorizons:ForestryMC:4.10.0:dev")
compileOnly("com.github.GTNewHorizons:Draconic-Evolution:1.3.14-GTNH:dev")
compileOnly("com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-503-GTNH:dev")
compileOnly("com.github.GTNewHorizons:EnderStorage:1.6.4:dev")
compileOnly("com.github.GTNewHorizons:GT5-Unofficial:5.09.51.13:dev")
devOnlyNonPublishable("com.github.GTNewHorizons:GT5-Unofficial:5.09.51.15:dev")
runtimeOnlyNonPublishable("com.github.GTNewHorizons:Botania:1.12.1-GTNH:dev")



Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cleanroommc/bogosorter/BogoSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void onPreInit(FMLPreInitializationEvent event) {
DefaultRules.init(BogoSortAPI.INSTANCE);
DefaultCompat.init(BogoSortAPI.INSTANCE);
Serializer.loadConfig();
MinecraftForge.EVENT_BUS.register(RefillHandler.class);
MinecraftForge.EVENT_BUS.register(new RefillHandler());
if (NetworkUtils.isDedicatedClient()) {
MinecraftForge.EVENT_BUS.post(new SortConfigChangeEvent());
FMLCommonHandler.instance().bus().register(new ClientEventHandler());
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/com/cleanroommc/bogosorter/ShortcutHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static void moveItemStack(EntityPlayer player, Container container, ISlot
if (toInsert == null) {
toInsert = stack.copy();
toInsert.stackSize -= (amount);
if (toInsert.stackSize == 0){
toInsert = null;
}
slot.bogo$putStack(toInsert);
// needed for crafting tables
slot.bogo$onSlotChanged(stack, toInsert);
Expand Down Expand Up @@ -100,9 +103,13 @@ public static void moveAllItems(EntityPlayer player, Container container, ISlot
continue;
ItemStack copy = stackInSlot.copy();
ItemStack remainder = BogoSortAPI.insert(container, otherSlots.getSlots(), copy);
int inserted = stackInSlot.stackSize - remainder.stackSize;
if (inserted > 0) {
slot1.bogo$putStack(remainder.stackSize > 0 ? remainder : null);
if (remainder == null){
slot1.bogo$putStack(null);
} else {
int inserted = stackInSlot.stackSize - remainder.stackSize;
if (inserted > 0) {
slot1.bogo$putStack(remainder.stackSize > 0 ? remainder : null);
}
}
}
}
Expand Down Expand Up @@ -150,15 +157,15 @@ public static ItemStack insert(ISlot slot, ItemStack stack, boolean emptyOnly) {
newStack.stackSize = (amount);
stack.stackSize -= (amount);
slot.bogo$putStack(newStack);
return stack == null ? null : stack;
return stack.stackSize == 0 ? null : stack;
}
if (stackInSlot != null && ItemHandlerHelper.canItemStacksStack(stackInSlot, stack)) {
int amount = Math.min(slot.bogo$getItemStackLimit(stackInSlot), Math.min(stack.stackSize, slot.bogo$getMaxStackSize(stack) - stackInSlot.stackSize));
if (amount <= 0) return stack;
stack.stackSize -=(amount);
stackInSlot.stackSize +=(amount);
slot.bogo$putStack(stackInSlot);
return stack == null ? null : stack;
return stack.stackSize == 0 ? null : stack;
}
return stack;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ public static String getMaterial(ItemStack item) {
@Optional.Method(modid = "gregtech")
@NotNull
public static String getGtToolMaterial(ItemStack itemStack) {
NBTTagCompound statsTag = itemStack.stackTagCompound.getCompoundTag("GT.Tool");
NBTTagCompound statsTag = itemStack.stackTagCompound.getCompoundTag("GT.ToolStats");
System.out.println(statsTag);
if (statsTag == null) {
return "";
}
if (statsTag.hasKey("Material")) {
return statsTag.getString("Material");
if (statsTag.hasKey("PrimaryMaterial")) {
return statsTag.getString("PrimaryMaterial");
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ public static boolean damageItemHook(ItemStack itemStack) {
if (!playerConfig.enableAutoRefill || playerConfig.autoRefillDamageThreshold <= 0) return false;

if (RefillHandler.shouldHandleRefill(player, itemStack) && isNotArmor(itemStack)) {
ItemStack handItem = player.getHeldItem();
if (handItem != itemStack) {
// handItem = player.getHeldItemOffhand();
// if (handItem != itemStack) {
// return false;
// }
return false;
}

int durabilityLeft = itemStack.getMaxDamage() - itemStack.getItemDamage();
if (durabilityLeft >= 0 && durabilityLeft < playerConfig.autoRefillDamageThreshold) {
return RefillHandler.handle(player.inventory.currentItem, itemStack, player, true);
Expand All @@ -34,11 +25,10 @@ public static boolean damageItemHook(ItemStack itemStack) {
return false;
}


private static boolean isNotArmor(ItemStack itemStack) {
if (itemStack.getItem() instanceof ItemArmor || itemStack.getItem() instanceof ISpecialArmor) return false;
// EntityEquipmentSlot slot = itemStack.getItem().getEquipmentSlot(itemStack);
// return slot == null || slot == EntityEquipmentSlot.MAINHAND || slot == EntityEquipmentSlot.OFFHAND;
return false;
return true;
}

public static int getDurability(ItemStack item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import com.cleanroommc.bogosorter.common.network.NetworkUtils;
import com.cleanroommc.bogosorter.common.network.SRefillSound;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import gregtech.api.items.GTGenericItem;
import gregtech.api.items.MetaGeneratedTool;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntListIterator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
Expand All @@ -22,18 +25,21 @@

public class RefillHandler {

private static final Class<?> gtToolClass;

static {
Class<?> clazz;
try {
clazz = Class.forName("gregtech.api.items.toolitem.IGTTool", false, RefillHandler.class.getClassLoader());
} catch (Exception ignored) {
clazz = null;
}
gtToolClass = clazz;
public RefillHandler(){
}

// private static final Class<?> gtToolClass;
//
// static {
// Class<?> clazz;
// try {
// clazz = Class.forName("gregtech.api.items.MetaGeneratedTool", false, RefillHandler.class.getClassLoader());
// } catch (Exception ignored) {
// clazz = null;
// }
// gtToolClass = clazz;
// }

private static final int[][] INVENTORY_PROXIMITY_MAP = {
{1, 2, 3, 4, 5, 6, 7, 8, 27, 18, 9, 28, 19, 10, 29, 20, 11, 30, 21, 12, 31, 22, 13, 32, 23, 14, 33, 24, 15, 34, 25, 16, 35, 26, 17},
{0, 2, 3, 4, 5, 6, 7, 8, 28, 19, 10, 27, 18, 9, 29, 20, 11, 30, 21, 12, 31, 22, 13, 32, 23, 14, 33, 24, 15, 34, 25, 16, 35, 26, 17},
Expand All @@ -48,31 +54,19 @@ public class RefillHandler {


@SubscribeEvent
public static void onDestroyItem(PlayerDestroyItemEvent event) {
public void onDestroyItem(PlayerDestroyItemEvent event) {
if (event.entityPlayer == null ||
event.entityPlayer.worldObj == null ||
event.entityPlayer.worldObj.isRemote ||
!PlayerConfig.get(event.entityPlayer).enableAutoRefill)
return;

if (event.original.getItem() instanceof ItemBlock && shouldHandleRefill(event.entityPlayer, event.original)) {
int index = event.entityPlayer.getHeldItem() == null ? event.entityPlayer.inventory.currentItem : 40;
if (event.original.getItem() != null && shouldHandleRefill(event.entityPlayer, event.original)) {
int index = event.entityPlayer.inventory.currentItem;
handle(index, event.original, event.entityPlayer, false);
}
}

/**
* Called via asm
*/
public static void onDestroyItem(EntityPlayer player, ItemStack brokenItem) {
if (!PlayerConfig.get(player).enableAutoRefill) return;

if (shouldHandleRefill(player, brokenItem)) {
int index = player.getHeldItem() != null ? player.inventory.currentItem : 40;
handle(index, brokenItem, player, false);
}
}

public static boolean handle(int hotbarIndex, ItemStack brokenItem, EntityPlayer player, boolean swap) {
return new RefillHandler(hotbarIndex, brokenItem, player, swap).handleRefill();
}
Expand All @@ -88,14 +82,16 @@ public static boolean shouldHandleRefill(EntityPlayer player, ItemStack brokenIt

private BiPredicate<ItemStack, ItemStack> similarItemMatcher = (stack, stack2) -> stack.getItem() == stack2.getItem() && stack.getItemDamage() == stack2.getItemDamage();
private BiPredicate<ItemStack, ItemStack> exactItemMatcher = RefillHandler::matchTags;
private final int hotbarIndex;
private final IntList slots;
private final ItemStack brokenItem;
private final EntityPlayer player;
private final InventoryPlayer inventory;
private final PlayerConfig playerConfig;
private final boolean swapItems;
private boolean isDamageable = false;
private int hotbarIndex;
private IntList slots;
private ItemStack brokenItem;
private EntityPlayer player;
private InventoryPlayer inventory;
private PlayerConfig playerConfig;
private boolean swapItems;
private boolean isDamageable;



public RefillHandler(int hotbarIndex, ItemStack brokenItem, EntityPlayer player, boolean swapItems) {
this.hotbarIndex = hotbarIndex;
Expand All @@ -107,20 +103,21 @@ public RefillHandler(int hotbarIndex, ItemStack brokenItem, EntityPlayer player,
this.swapItems = swapItems;
}


public RefillHandler(int hotbarIndex, ItemStack brokenItem, EntityPlayer player) {
this(hotbarIndex, brokenItem, player, false);
}

public boolean handleRefill() {
if (brokenItem.getItem() instanceof ItemBlock) {
return findItem(false);
} else if (brokenItem.isItemStackDamageable()) {
if (gtToolClass != null ) { // && isGTCEuTool(brokenItem))
exactItemMatcher = (stack, stack2) -> {
if (stack.hasTagCompound() != stack2.hasTagCompound()) return false;
if (!stack.hasTagCompound()) return true;
return OreDictHelper.getGtToolMaterial(stack).equals(OreDictHelper.getGtToolMaterial(stack2));
};
} else if (brokenItem.isItemStackDamageable() || brokenItem.getItem() instanceof GTGenericItem) {
if (brokenItem.getItem() instanceof MetaGeneratedTool) {
exactItemMatcher = (stack, stack2) -> {
if (stack.hasTagCompound() != stack2.hasTagCompound()) return false;
if (!stack.hasTagCompound()) return true;
return OreDictHelper.getGtToolMaterial(stack).equals(OreDictHelper.getGtToolMaterial(stack2));
};
} else {
similarItemMatcher = (stack, stack2) -> stack.getItem() == stack2.getItem();
}
Expand All @@ -131,9 +128,9 @@ public boolean handleRefill() {
}
}

// private static boolean isGTCEuTool(ItemStack itemStack) {
// return itemStack.getItem() instanceof IGTTool;
// }
private static boolean isGT5uTool(ItemStack itemStack) {
return itemStack.getItem() instanceof MetaGeneratedTool;
}

private boolean findItem(boolean exactOnly) {
ItemStack firstItemMatch = null;
Expand Down Expand Up @@ -212,6 +209,7 @@ private void refillItem(ItemStack refill, int refillIndex) {
if (!this.swapItems) current = getItem(this.hotbarIndex);
setAndSyncSlot(hotbarIndex, refill.copy());
setAndSyncSlot(refillIndex, swapItems ? brokenItem.copy() : null);
player.inventoryContainer.detectAndSendChanges();
if (current != null) {
// the broken item replaced itself with something
// insert the item into another slot to prevent it from being lost
Expand All @@ -235,10 +233,6 @@ private void setAndSyncSlot(int index, ItemStack item) {
inventory.armorInventory[index - 36] = item;
slot += 5;
}
// else {
// inventory.offHandInventory.set(0, item);
// slot = 45;
// }
if (item != null) {
player.inventoryContainer.inventoryItemStacks.set(slot, null);
}
Expand Down

This file was deleted.

Loading
Loading