Skip to content

Commit

Permalink
Allow screwdriver in toolbox for programmed circuit (#3775)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <[email protected]>
  • Loading branch information
serenibyss and Dream-Master authored Jan 13, 2025
1 parent 5fc14dc commit cb48e82
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 102 deletions.
11 changes: 4 additions & 7 deletions src/main/java/bartworks/common/items/ItemCircuitProgrammer.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import bartworks.util.BWUtil;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.GregTechAPI;
import gregtech.api.enums.GTValues;
import gregtech.api.enums.ItemList;
import gregtech.api.enums.Materials;
Expand All @@ -63,12 +62,10 @@ public ItemCircuitProgrammer() {
this.setNoRepair();
this.setHasSubtypes(false);
this.setCreativeTab(MainMod.BWT);
GregTechAPI.registerCircuitProgrammer(
s -> s.getItem() instanceof ItemCircuitProgrammer && ElectricItem.manager.canUse(s, COST_PER_USE),
(s, p) -> {
ElectricItem.manager.use(s, COST_PER_USE, p);
return s;
});
}

public void useItem(ItemStack stack, EntityPlayer player) {
ElectricItem.manager.use(stack, COST_PER_USE, player);
}

@Override
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/gregtech/GTMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static gregtech.GT_Version.VERSION_MAJOR;
import static gregtech.GT_Version.VERSION_MINOR;
import static gregtech.GT_Version.VERSION_PATCH;
import static gregtech.api.GregTechAPI.registerCircuitProgrammer;
import static gregtech.api.enums.Mods.Forestry;
import static gregtech.api.util.GTRecipe.setItemStacks;

Expand All @@ -13,7 +12,6 @@
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.function.Predicate;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
Expand All @@ -26,7 +24,6 @@
import net.minecraft.util.WeightedRandomChestContent;
import net.minecraft.world.World;
import net.minecraftforge.common.ChestGenHooks;
import net.minecraftforge.oredict.OreDictionary;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -354,17 +351,6 @@ public void onLoad(FMLInitializationEvent aEvent) {

gregtechproxy.onLoad();

registerCircuitProgrammer(new Predicate<>() {

private final int screwdriverOreId = OreDictionary.getOreID("craftingToolScrewdriver");

@Override
public boolean test(ItemStack stack) {
for (int i : OreDictionary.getOreIDs(stack)) if (i == screwdriverOreId) return true;
return false;
}
}, true);

new MTERecipeLoader().run();

new GTItemIterator().run();
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/gregtech/api/GregTechAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
Expand Down Expand Up @@ -163,9 +159,6 @@ public class GregTechAPI {
private static final Multimap<Integer, ItemStack> sRealConfigurationList = Multimaps
.newListMultimap(new TreeMap<>(), ArrayList::new);
private static final Map<Integer, List<ItemStack>> sConfigurationLists = new ConcurrentHashMap<>();
private static final Map<Predicate<ItemStack>, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> sRealCircuitProgrammerList = new LinkedHashMap<>();
public static final Map<Predicate<ItemStack>, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> sCircuitProgrammerList = Collections
.unmodifiableMap(sRealCircuitProgrammerList);

/**
* The List of Dimensions, which are Whitelisted for the Teleporter. This list should not contain other Planets.
Expand Down Expand Up @@ -449,22 +442,6 @@ public static Comparator<ItemStack> getConfigurationCircuitsComparator() {
.thenComparing(ItemStack::getItemDamage);
}

public static void registerCircuitProgrammer(ItemStack stack, boolean ignoreNBT, boolean useContainer) {
registerCircuitProgrammer(rhs -> GTUtility.areStacksEqual(stack, rhs, ignoreNBT), useContainer);
}

public static void registerCircuitProgrammer(Predicate<ItemStack> predicate, boolean useContainer) {
sRealCircuitProgrammerList.put(
predicate,
useContainer ? (s, p) -> s.getItem()
.getContainerItem(s) : (s, p) -> s);
}

public static void registerCircuitProgrammer(Predicate<ItemStack> predicate,
BiFunction<ItemStack, EntityPlayerMP, ItemStack> doDamage) {
sRealCircuitProgrammerList.put(predicate, doDamage);
}

public static void registerCover(ItemStack aStack, ITexture aCover, CoverBehavior aBehavior) {
registerCover(aStack, aCover, (CoverBehaviorBase<?>) aBehavior);
}
Expand Down
109 changes: 85 additions & 24 deletions src/main/java/gregtech/common/items/ItemIntegratedCircuit.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Predicate;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
Expand All @@ -16,18 +13,19 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.FakePlayer;

import org.apache.commons.lang3.tuple.Pair;
import net.minecraftforge.oredict.OreDictionary;

import com.gtnewhorizons.modularui.api.UIInfos;

import bartworks.common.items.ItemCircuitProgrammer;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.GregTechAPI;
Expand All @@ -43,8 +41,10 @@
import gregtech.api.util.GTLanguageManager;
import gregtech.api.util.GTLog;
import gregtech.api.util.GTModHandler;
import gregtech.api.util.GTUtility;
import gregtech.common.gui.modularui.uifactory.SelectItemUIFactory;
import ic2.core.IC2;
import ic2.core.IHasGui;
import ic2.core.item.ItemToolbox;

public class ItemIntegratedCircuit extends GTGenericItem implements INetworkUpdatableItem {

Expand Down Expand Up @@ -246,12 +246,7 @@ public boolean receive(ItemStack stack, EntityPlayerMP player, NBTTagCompound ta
if (meta < 0 || meta > 24) return true;

if (!player.capabilities.isCreativeMode) {
Pair<Integer, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> toolIndex = findConfiguratorInInv(player);
if (toolIndex == null) return true;

ItemStack[] mainInventory = player.inventory.mainInventory;
mainInventory[toolIndex.getKey()] = toolIndex.getValue()
.apply(mainInventory[toolIndex.getKey()], player);
findConfiguratorInInv(player, true); // damage the tool
}
stack.setItemDamage(meta);

Expand All @@ -267,8 +262,8 @@ public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer pla
if (player.capabilities.isCreativeMode) {
configuratorStack = null;
} else {
Pair<Integer, ?> configurator = findConfiguratorInInv(player);
if (configurator == null) {
configuratorStack = findConfiguratorInInv(player, false);
if (configuratorStack == null) {
int count;
try {
count = Integer
Expand All @@ -285,7 +280,6 @@ public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer pla
"GT5U.item.programmed_circuit.no_screwdriver." + XSTR.XSTR_INSTANCE.nextInt(count)));
return stack;
}
configuratorStack = player.inventory.mainInventory[configurator.getKey()];
}
openSelectorGui(configuratorStack, stack.getItemDamage(), player);
return stack;
Expand All @@ -309,19 +303,86 @@ private static void onConfigured(ItemStack stack) {
GTValues.NW.sendToServer(new GTPacketUpdateItem(tag));
}

private static Pair<Integer, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> findConfiguratorInInv(
EntityPlayer player) {
private static final int screwdriverOreId = OreDictionary.getOreID("craftingToolScrewdriver");

/**
* Test a player's inventory for a circuit configuring item.
*
* @param player The player whose inventory will be tested.
* @param doDamage Whether a found item should be damaged.
*
* @return A display stack representing the item that was found. Do not modify this item!
*/
public static ItemStack findConfiguratorInInv(EntityPlayer player, boolean doDamage) {
ItemStack[] mainInventory = player.inventory.mainInventory;
for (int j = 0, mainInventoryLength = mainInventory.length; j < mainInventoryLength; j++) {
ItemStack toolStack = mainInventory[j];
for (int i = 0; i < mainInventory.length; i++) {
ItemStack potentialStack = mainInventory[i];
if (potentialStack == null || potentialStack.getItem() == null || potentialStack.stackSize <= 0) continue;

// Circuit Configurator
if (potentialStack.getItem() instanceof ItemCircuitProgrammer programmer) {
if (doDamage) programmer.useItem(potentialStack, player);
return potentialStack;
}

if (!GTUtility.isStackValid(toolStack)) continue;
// Toolbox with Screwdriver inside
if (potentialStack.getItem() instanceof ItemToolbox toolbox) {
IHasGui toolboxInventory = toolbox.getInventory(player, potentialStack);
if (!IC2.platform.isSimulating()) {
populateToolboxInventory(toolboxInventory, potentialStack);
}
for (int j = 0; j < toolboxInventory.getSizeInventory(); j++) {
ItemStack toolboxStack = toolboxInventory.getStackInSlot(j);
if (toolboxStack == null || toolboxStack.getItem() == null || toolboxStack.stackSize <= 0) continue;

for (Map.Entry<Predicate<ItemStack>, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> p : GregTechAPI.sCircuitProgrammerList
.entrySet())
if (p.getKey()
.test(toolStack)) return Pair.of(j, p.getValue());
for (int id : OreDictionary.getOreIDs(toolboxStack)) {
if (id == screwdriverOreId) {
if (doDamage) {
toolboxStack = toolboxStack.getItem()
.getContainerItem(toolboxStack);
if (toolboxStack != null && toolboxStack.stackSize <= 0) {
toolboxInventory.setInventorySlotContents(j, null);
} else {
toolboxInventory.setInventorySlotContents(j, toolboxStack);
}
}
return potentialStack; // return the toolbox for display
}
}
}
}

// Screwdriver
for (int id : OreDictionary.getOreIDs(potentialStack)) {
if (id == screwdriverOreId) {
if (doDamage) {
potentialStack = potentialStack.getItem()
.getContainerItem(potentialStack);
if (potentialStack != null && potentialStack.stackSize <= 0) {
mainInventory[i] = null;
} else {
mainInventory[i] = potentialStack;
}
}
return potentialStack;
}
}
}
return null;
}

// Because for some reason, the toolbox inventory refuses to read the itemstack nbt
// on the client (according to IC2 this is in fact intentional).
private static void populateToolboxInventory(IHasGui toolboxInventory, ItemStack toolbox) {
NBTTagCompound nbt = toolbox.getTagCompound();
if (nbt == null) return;
NBTTagList stacks = nbt.getTagList("Items", 10);
for (int i = 0; i < stacks.tagCount(); i++) {
NBTTagCompound slotNbt = stacks.getCompoundTagAt(i);
int slot = slotNbt.getByte("Slot");
if (slot >= 0 && slot < toolboxInventory.getSizeInventory()) {
toolboxInventory.setInventorySlotContents(i, ItemStack.loadItemStackFromNBT(slotNbt));
}
}
}
}
Loading

0 comments on commit cb48e82

Please sign in to comment.