mirror of
https://github.com/songoda/EpicHoppers.git
synced 2025-02-16 20:01:42 +01:00
Add caching for auto smelt module
This commit is contained in:
parent
5bfd4692cd
commit
fa6e0bf44e
@ -10,24 +10,35 @@ import com.craftaro.epichoppers.hopper.HopperImpl;
|
|||||||
import com.craftaro.epichoppers.settings.Settings;
|
import com.craftaro.epichoppers.settings.Settings;
|
||||||
import com.craftaro.epichoppers.gui.GUISmeltable;
|
import com.craftaro.epichoppers.gui.GUISmeltable;
|
||||||
import com.craftaro.epichoppers.utils.StorageContainerCache;
|
import com.craftaro.epichoppers.utils.StorageContainerCache;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.inventory.ClickType;
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
import org.bukkit.inventory.FurnaceRecipe;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.Recipe;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class ModuleAutoSmelter extends Module {
|
public class ModuleAutoSmelter extends Module {
|
||||||
private final int timeOut;
|
private final int timeOut;
|
||||||
private final int hopperTickRate;
|
private final int hopperTickRate;
|
||||||
|
private final Map<XMaterial, ItemStack> RECIPE_CACHE;
|
||||||
|
|
||||||
public ModuleAutoSmelter(SongodaPlugin plugin, GuiManager guiManager, int timeOut) {
|
public ModuleAutoSmelter(SongodaPlugin plugin, GuiManager guiManager, int timeOut) {
|
||||||
super(plugin, guiManager);
|
super(plugin, guiManager);
|
||||||
this.timeOut = timeOut * 20;
|
this.timeOut = timeOut * 20;
|
||||||
this.hopperTickRate = Settings.HOP_TICKS.getInt();
|
this.hopperTickRate = Settings.HOP_TICKS.getInt();
|
||||||
|
this.RECIPE_CACHE = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -60,7 +71,7 @@ public class ModuleAutoSmelter extends Module {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
XMaterial input = CompatibleMaterial.getMaterial(itemStack.getType()).get();
|
XMaterial input = CompatibleMaterial.getMaterial(itemStack.getType()).get();
|
||||||
ItemStack result = CompatibleMaterial.getFurnaceResult(input);
|
ItemStack result = getFurnaceResult(input);
|
||||||
|
|
||||||
if (hopperCache.addItem(result)) {
|
if (hopperCache.addItem(result)) {
|
||||||
if (itemStack.getAmount() == 1) {
|
if (itemStack.getAmount() == 1) {
|
||||||
@ -80,6 +91,29 @@ public class ModuleAutoSmelter extends Module {
|
|||||||
modifyDataCache(hopper, "time", subtract);
|
modifyDataCache(hopper, "time", subtract);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private @Nullable ItemStack getFurnaceResult(XMaterial material) {
|
||||||
|
if (RECIPE_CACHE.containsKey(material)) {
|
||||||
|
return RECIPE_CACHE.get(material);
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator<Recipe> recipes = Bukkit.recipeIterator();
|
||||||
|
|
||||||
|
while(recipes.hasNext()) {
|
||||||
|
Recipe recipe = (Recipe)recipes.next();
|
||||||
|
if (recipe instanceof FurnaceRecipe) {
|
||||||
|
FurnaceRecipe furnaceRecipe = (FurnaceRecipe)recipe;
|
||||||
|
if (material.isSimilar(furnaceRecipe.getInput())) {
|
||||||
|
RECIPE_CACHE.put(material, furnaceRecipe.getInput());
|
||||||
|
return furnaceRecipe.getResult();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack getGUIButton(Hopper hopper) {
|
public ItemStack getGUIButton(Hopper hopper) {
|
||||||
ItemStack block = XMaterial.IRON_INGOT.parseItem();
|
ItemStack block = XMaterial.IRON_INGOT.parseItem();
|
||||||
|
Loading…
Reference in New Issue
Block a user