Skip to content

Commit

Permalink
Merge pull request #494 from CivMC/revert-factorymod
Browse files Browse the repository at this point in the history
Revert "Merge pull request #394 from MrJeremyFisher/maybe-fix-factory…
  • Loading branch information
okx-code authored May 8, 2024
2 parents 87d3605 + b52671a commit 854e657
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import net.minecraft.nbt.ByteArrayTag;
import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -22,21 +31,11 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import vg.civcraft.mc.civmodcore.inventory.ClonedInventory;
import org.bukkit.inventory.meta.Repairable;
import vg.civcraft.mc.civmodcore.nbt.NBTSerialization;
import vg.civcraft.mc.civmodcore.utilities.CivLogger;
import vg.civcraft.mc.civmodcore.utilities.MoreMath;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

/**
* Allows the storage and comparison of item stacks while ignoring their maximum possible stack sizes. This offers
* various tools to compare inventories, to store recipe costs or to specify setup costs. Take great care when dealing
Expand Down Expand Up @@ -496,10 +495,10 @@ public ItemMap clone() {
}

/**
* Checks whether this instance would completely fit into the given inventory
* Checks whether this instance would completly fit into the given inventory
*
* @param i Inventory to check
* @return True if this ItemMap's item representation would completely fit in the inventory, false if not
* @return True if this ItemMap's item representation would completly fit in the inventory, false if not
*/
public boolean fitsIn(Inventory i) {
int size;
Expand All @@ -510,24 +509,11 @@ public boolean fitsIn(Inventory i) {
}
ItemMap invCopy = new ItemMap();
for (ItemStack is : i.getStorageContents()) {
if (is == null) continue;
invCopy.addItemAmount(is, is.getAmount());
invCopy.addItemStack(is);
}
ItemMap instanceCopy = this.clone();
instanceCopy.merge(invCopy);

if (!(instanceCopy.getItemStackRepresentation().size() <= size)) {
return false;
}

ClonedInventory clonedInventory = ClonedInventory.cloneInventory(i);

for(ItemStack itemStack : this.getItemStackRepresentation()) {
if (!clonedInventory.addItem(itemStack).isEmpty()) {
return false;
}
}
return true;
return instanceCopy.getItemStackRepresentation().size() <= size;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.github.igotyou.FactoryMod.events.RecipeExecuteEvent;
import com.github.igotyou.FactoryMod.interactionManager.IInteractionManager;
import com.github.igotyou.FactoryMod.powerManager.FurnacePowerManager;
import com.github.igotyou.FactoryMod.utility.Direction;
import com.github.igotyou.FactoryMod.utility.IIOFInventoryProvider;
import com.github.igotyou.FactoryMod.powerManager.IPowerManager;
import com.github.igotyou.FactoryMod.recipes.IRecipe;
import com.github.igotyou.FactoryMod.recipes.InputRecipe;
Expand All @@ -15,10 +17,16 @@
import com.github.igotyou.FactoryMod.repairManager.IRepairManager;
import com.github.igotyou.FactoryMod.repairManager.PercentageHealthRepairManager;
import com.github.igotyou.FactoryMod.structures.FurnCraftChestStructure;
import com.github.igotyou.FactoryMod.utility.Direction;
import com.github.igotyou.FactoryMod.utility.IIOFInventoryProvider;
import com.github.igotyou.FactoryMod.utility.IOSelector;
import com.github.igotyou.FactoryMod.utility.LoggingUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

import com.github.igotyou.FactoryMod.utility.MultiInventoryWrapper;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand All @@ -38,14 +46,6 @@
import vg.civcraft.mc.namelayer.NameAPI;
import vg.civcraft.mc.namelayer.permission.PermissionType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

/**
* Represents a "classic" factory, which consists of a furnace as powersource, a
* crafting table as main interaction element between the furnace and the chest,
Expand Down Expand Up @@ -335,7 +335,7 @@ public void attemptToActivate(Player p, boolean onStartUp) {
}

// Ensure the recipe effect can be applied
var effectFeasibility = currentRecipe.evaluateEffectFeasibility(getInputInventory(), getOutputInventory(), this);
var effectFeasibility = currentRecipe.evaluateEffectFeasibility(getInputInventory(), getOutputInventory());
if (!(effectFeasibility.isFeasible())) {
LoggingUtils.log(String.format("Skipping activation of recipe [%s], since the effect wasn't feasible.", currentRecipe.getName()));
if (p != null) {
Expand Down Expand Up @@ -522,6 +522,7 @@ public void run() {
deactivate();
return;
}
sendActivatorMessage(ChatColor.GOLD + currentRecipe.getName() + " in " + name + " completed");
if (currentRecipe instanceof Upgraderecipe || currentRecipe instanceof RecipeScalingUpgradeRecipe) {
// this if else might look a bit weird, but because
// upgrading changes the current recipe and a lot of
Expand All @@ -531,7 +532,6 @@ public void run() {
return;
} else {
if (currentRecipe.applyEffect(getInputInventory(), getOutputInventory(), this)) {
sendActivatorMessage(ChatColor.GOLD + currentRecipe.getName() + " in " + name + " completed");
runCount.put(currentRecipe, runCount.get(currentRecipe) + 1);
} else {
sendActivatorMessage(ChatColor.RED + currentRecipe.getName() + " in " + name + " deactivated because it ran out of storage space");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.github.igotyou.FactoryMod.recipes;

import com.github.igotyou.FactoryMod.factories.FurnCraftChestFactory;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

import com.github.igotyou.FactoryMod.utility.MultiInventoryWrapper;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
Expand All @@ -9,11 +14,6 @@
import vg.civcraft.mc.civmodcore.inventory.items.ItemMap;
import vg.civcraft.mc.civmodcore.inventory.items.ItemUtils;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
* Used to decompact itemstacks, which means a single item with compacted lore
* is turned into a whole stack without lore. This reverses the functionality of
Expand Down Expand Up @@ -45,15 +45,15 @@ public boolean enoughMaterialAvailable(Inventory inputInv) {
}

@Override
public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv, FurnCraftChestFactory fccf) {
public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv) {
boolean isFeasible = Arrays.stream(inputInv.getContents())
.filter(Objects::nonNull)
.filter(this::isDecompactable)
.map(it -> {
ItemStack removeClone = it.clone();
removeClone.setAmount(1);
removeCompactLore(removeClone);
ItemMap toAdd = new ItemMap();
ItemMap toAdd = new ItemMap(removeClone);
toAdd.addItemAmount(removeClone, CompactingRecipe.getCompactStackSize(removeClone.getType()));
return toAdd;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface IRecipe {
* input/output inventories, or other custom recipe logic.
* By default, this method returns a result indicating that the effect is always feasible to be applied.
*/
default public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv, FurnCraftChestFactory fccf) {
default public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv) {
return new EffectFeasibility(true, null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.github.igotyou.FactoryMod.recipes;

import com.github.igotyou.FactoryMod.factories.FurnCraftChestFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.github.igotyou.FactoryMod.utility.MultiInventoryWrapper;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import vg.civcraft.mc.civmodcore.inventory.ClonedInventory;
import vg.civcraft.mc.civmodcore.inventory.items.ItemMap;
import vg.civcraft.mc.civmodcore.inventory.items.ItemUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/***
* Outputs a player head belonging to a random player who is connected to the server when the recipe is run
*/
Expand Down Expand Up @@ -59,23 +58,17 @@ public boolean applyEffect(Inventory inputInv, Inventory outputInv, FurnCraftChe
if (players.isEmpty()) {
return false;
}

if (toRemove.isContainedIn(inputInv)) {
Random rand = new Random();
Player player = players.get(rand.nextInt(players.size()));
ItemStack is = new ItemStack(Material.PLAYER_HEAD, 1);

if (!ClonedInventory.cloneInventory(outputInv).addItem(is).isEmpty()) {
return false;
}
if (toRemove.removeSafelyFrom(inputInv)) {
Random rand = new Random();
Player player = players.get(rand.nextInt(players.size()));
ItemStack is = new ItemStack(Material.PLAYER_HEAD, 1);
SkullMeta im = (SkullMeta) is.getItemMeta();
im.setOwningPlayer(Bukkit.getOfflinePlayer(player.getUniqueId()));
im.setDisplayName(player.getDisplayName());
is.setItemMeta(im);
outputInv.addItem(is);
}

}
logAfterRecipeRun(combo, fccf);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import com.github.igotyou.FactoryMod.factories.FurnCraftChestFactory;
import com.github.igotyou.FactoryMod.utility.MultiInventoryWrapper;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import net.minecraft.nbt.CompoundTag;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand All @@ -11,14 +16,10 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;
import vg.civcraft.mc.civmodcore.inventory.ClonedInventory;
import vg.civcraft.mc.civmodcore.inventory.items.ItemMap;
import vg.civcraft.mc.civmodcore.inventory.items.ItemUtils;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PrintBookRecipe extends PrintingPressRecipe {
private ItemMap printingPlate;
Expand Down Expand Up @@ -57,16 +58,12 @@ public boolean applyEffect(Inventory inputInv, Inventory outputInv, FurnCraftChe

ItemStack printingPlateStack = getPrintingPlateItemStack(inputInv, this.printingPlate);
ItemMap toRemove = this.input.clone();

ItemStack book = createBook(printingPlateStack, this.outputAmount);
if (!ClonedInventory.cloneInventory(outputInv).addItem(book).isEmpty()) {
return false;
}


if (printingPlateStack != null
&& toRemove.isContainedIn(inputInv)
&& toRemove.removeSafelyFrom(inputInv)
) {
ItemStack book = createBook(printingPlateStack, this.outputAmount);
book.editMeta(BookMeta.class, x -> x.setGeneration(getNextGeneration(x.getGeneration())));
outputInv.addItem(book);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@

import com.github.igotyou.FactoryMod.factories.FurnCraftChestFactory;
import com.github.igotyou.FactoryMod.utility.MultiInventoryWrapper;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.nbt.CompoundTag;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;
import vg.civcraft.mc.civmodcore.inventory.ClonedInventory;
import vg.civcraft.mc.civmodcore.inventory.items.ItemMap;
import vg.civcraft.mc.civmodcore.inventory.items.ItemUtils;

import java.util.ArrayList;
import java.util.List;

public class PrintNoteRecipe extends PrintBookRecipe {
private static class BookInfo {
public String title;
Expand Down Expand Up @@ -68,18 +69,14 @@ public boolean applyEffect(Inventory inputInv, Inventory outputInv, FurnCraftChe

ItemStack printingPlateStack = getPrintingPlateItemStack(inputInv, getPrintingPlate());
ItemMap toRemove = this.input.clone();

ItemStack paper = new ItemStack(Material.PAPER, getOutputAmount());
if (!ClonedInventory.cloneInventory(outputInv).addItem(paper).isEmpty()) {
return false;
}


if (printingPlateStack != null
&& toRemove.isContainedIn(inputInv)
&& toRemove.removeSafelyFrom(inputInv)
) {
BookInfo info = getBookInfo(printingPlateStack);

ItemStack paper = new ItemStack(Material.PAPER, getOutputAmount());

ItemMeta paperMeta = paper.getItemMeta();
paperMeta.setDisplayName(ChatColor.RESET + info.title);
paperMeta.setLore(info.lines);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import java.util.Arrays;
import java.util.UUID;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
Expand All @@ -22,9 +24,6 @@
import vg.civcraft.mc.civmodcore.inventory.items.ItemMap;
import vg.civcraft.mc.civmodcore.inventory.items.ItemUtils;

import java.util.Arrays;
import java.util.UUID;

public class PrintingPlateJsonRecipe extends PrintingPlateRecipe {
/*
* WARNING
Expand Down Expand Up @@ -96,10 +95,6 @@ public boolean applyEffect(Inventory inputInv, Inventory outputInv, FurnCraftChe
ItemMap toRemove = input.clone();
ItemMap toAdd = output.clone();

if (!toAdd.fitsIn(outputInv)) {
return false;
}

if (toRemove.isContainedIn(inputInv) && toRemove.removeSafelyFrom(inputInv)) {
for (ItemStack is : toAdd.getItemStackRepresentation()) {
is = addTags(serialNumber, is, bookNBT);
Expand Down
Loading

0 comments on commit 854e657

Please sign in to comment.