Skip to content

Commit

Permalink
Big updates for API
Browse files Browse the repository at this point in the history
- add a method to remove a machine
- change a bunch of things from Furnace to Machine
  • Loading branch information
ShaneBeee committed Dec 27, 2024
1 parent 1c68d58 commit deecbbd
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 72 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/shanebeestudios/vf/VirtualFurnace.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.shanebeestudios.vf.api.VirtualFurnaceAPI;
import com.shanebeestudios.vf.api.recipe.Fuel;
import com.shanebeestudios.vf.api.recipe.FurnaceRecipe;
import com.shanebeestudios.vf.api.task.FurnaceTick;
import com.shanebeestudios.vf.api.util.Util;
import com.shanebeestudios.vf.command.FurnaceCommand;
import org.bukkit.Bukkit;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/shanebeestudios/vf/api/FurnaceListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.shanebeestudios.vf.api.chunk.VirtualChunk;
import com.shanebeestudios.vf.api.event.machine.FurnaceExtractEvent;
import com.shanebeestudios.vf.api.machine.Furnace;
import com.shanebeestudios.vf.api.machine.Machine;
import com.shanebeestudios.vf.api.recipe.Fuel;
import com.shanebeestudios.vf.api.tile.Tile;
import org.bukkit.Chunk;
Expand Down Expand Up @@ -40,10 +41,10 @@ private void onClickFurnace(PlayerInteractEvent event) {
ItemStack hand = event.getItem();
Player player = event.getPlayer();
if (hand != null) {
Furnace furnace = this.furnaceManager.getFurnaceFromItemStack(hand);
if (furnace != null) {
Machine machine = this.furnaceManager.getMachineFromItemStack(hand);
if (machine != null) {
event.setCancelled(true);
furnace.openInventory(player);
machine.openInventory(player);
return;
}
}
Expand Down
127 changes: 90 additions & 37 deletions src/main/java/com/shanebeestudios/vf/api/FurnaceManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.shanebeestudios.vf.api;

import com.shanebeestudios.vf.api.machine.Furnace;
import com.shanebeestudios.vf.api.machine.Machine;
import com.shanebeestudios.vf.api.property.FurnaceProperties;
import com.shanebeestudios.vf.api.util.Util;
import org.bukkit.Material;
Expand All @@ -19,16 +20,18 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;

/**
* Manager for {@link Furnace Furnaces}
* Manager for {@link Machine Machines}
* <p>You can get an instance of this class from <b>{@link VirtualFurnaceAPI#getFurnaceManager()}</b></p>
*/
@SuppressWarnings({"unused", "UnusedReturnValue"})
Expand All @@ -41,35 +44,52 @@ public class FurnaceManager {
private static final @NotNull Enchantment INFINITY = Registry.ENCHANTMENT.get(NamespacedKey.minecraft("infinity"));

private final VirtualFurnaceAPI virtualFurnaceAPI;
private File furnaceFile;
private FileConfiguration furnaceConfig;
private final Map<UUID, Furnace> furnaceMap;
private File machineFile;
private FileConfiguration machineConfig;
private final Map<UUID, Machine> machineMap;
private final NamespacedKey key;

FurnaceManager(VirtualFurnaceAPI virtualFurnaceAPI) {
this.virtualFurnaceAPI = virtualFurnaceAPI;
this.furnaceMap = new HashMap<>();
this.machineMap = new HashMap<>();
this.key = new NamespacedKey(virtualFurnaceAPI.getJavaPlugin(), "furnaceID");
loadFurnaceConfig();
}

/**
* Get a collection of all {@link Furnace}s
* Get a collection of all {@link Furnace Furnaces}
*
* @return Collection of all furnaces
* @deprecated Use {@link #getAllMachines()} instead
*/
@Deprecated
public Collection<Furnace> getAllFurnaces() {
return Collections.unmodifiableCollection(this.furnaceMap.values());
List<Furnace> furnaces = new ArrayList<>();
for (Machine value : this.machineMap.values()) {
if (value instanceof Furnace furnace) {
furnaces.add(furnace);
}
}
return furnaces;
}

/**
* Get a collection of all {@link Machine Machines}
*
* @return Collection of all machines
*/
public Collection<Machine> getAllMachines() {
return Collections.unmodifiableCollection(this.machineMap.values());
}

/**
* Get a {@link Furnace} by ID
* Get a {@link Machine} by ID
*
* @param uuid ID of furnace to grab
* @return Furnace from ID (null if a furnace with this ID does not exist)
* @param uuid ID of machine to grab
* @return Machine from ID (null if a machine with this ID does not exist)
*/
public Furnace getByID(@NotNull UUID uuid) {
return this.furnaceMap.get(uuid);
public Machine getByID(@NotNull UUID uuid) {
return this.machineMap.get(uuid);
}

/**
Expand Down Expand Up @@ -123,7 +143,7 @@ public Furnace createFurnace(@NotNull String name, @NotNull FurnaceProperties fu
if (function != null) {
function.accept(furnace);
}
this.furnaceMap.put(furnace.getUniqueID(), furnace);
this.machineMap.put(furnace.getUniqueID(), furnace);
saveFurnace(furnace, true);
return furnace;
}
Expand Down Expand Up @@ -238,12 +258,14 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnacePro
if (glowing) {
if (HAS_GLINT) {
meta.setEnchantmentGlintOverride(true);
} else if (item.getType() == Material.ARROW) {
meta.addEnchant(SHARPNESS, 1, true);
} else {
meta.addEnchant(INFINITY, 1, true);
if (item.getType() == Material.ARROW) {
meta.addEnchant(SHARPNESS, 1, true);
} else {
meta.addEnchant(INFINITY, 1, true);
}
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
Furnace furnace;
if (function == null) {
Expand All @@ -261,8 +283,27 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnacePro
*
* @param itemStack ItemStack to grab furnace from
* @return Furnace if the ItemStack has one assigned to it else null
* @deprecated Use {@link #getMachineFromItemStack(ItemStack)} instead
*/
@Deprecated
public Furnace getFurnaceFromItemStack(@NotNull ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (meta != null && meta.getPersistentDataContainer().has(this.key, PersistentDataType.STRING)) {
String u = meta.getPersistentDataContainer().get(this.key, PersistentDataType.STRING);
if (u == null) return null;
Machine machine = getByID(UUID.fromString(u));
if (machine instanceof Furnace furnace) return furnace;
}
return null;
}

/**
* Get a {@link Machine} from an {@link ItemStack}
*
* @param itemStack ItemStack to grab machine from
* @return Machine if the ItemStack has one assigned to it else null
*/
public Machine getMachineFromItemStack(@NotNull ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (meta != null && meta.getPersistentDataContainer().has(this.key, PersistentDataType.STRING)) {
String u = meta.getPersistentDataContainer().get(this.key, PersistentDataType.STRING);
Expand All @@ -273,64 +314,76 @@ public Furnace getFurnaceFromItemStack(@NotNull ItemStack itemStack) {
}

private void loadFurnaceConfig() {
if (this.furnaceFile == null) {
this.furnaceFile = new File(this.virtualFurnaceAPI.getJavaPlugin().getDataFolder(), "furnaces.yml");
if (this.machineFile == null) {
this.machineFile = new File(this.virtualFurnaceAPI.getJavaPlugin().getDataFolder(), "furnaces.yml");
}
if (!furnaceFile.exists()) {
if (!machineFile.exists()) {
this.virtualFurnaceAPI.getJavaPlugin().saveResource("furnaces.yml", false);
}
this.furnaceConfig = YamlConfiguration.loadConfiguration(this.furnaceFile);
this.machineConfig = YamlConfiguration.loadConfiguration(this.machineFile);
loadFurnaces();
}

void loadFurnaces() {
ConfigurationSection section = this.furnaceConfig.getConfigurationSection("furnaces");
ConfigurationSection section = this.machineConfig.getConfigurationSection("furnaces");
if (section != null) {
for (String string : section.getKeys(true)) {
if (section.get(string) instanceof Furnace furnace) {
this.furnaceMap.put(UUID.fromString(string), (Furnace) section.get(string));
this.machineMap.put(UUID.fromString(string), furnace);
}
}
}
Util.log("Loaded: &b" + this.furnaceMap.size() + "&7 furnaces");
Util.log("Loaded: &b" + this.machineMap.size() + "&7 furnaces");
}

/**
* Save a furnace to YAML storage
* Save a machine to YAML storage
* <p><b>NOTE:</b> If choosing not to save to file, this change will not take effect
* in the YAML file, this may be useful for saving a large batch and saving file at the
* end of the batch change, use {@link #saveConfig()} to save all changes to file</p>
*
* @param furnace Furnace to save
* @param machine Machine to save
* @param saveToFile Whether to save to file
*/
public void saveFurnace(@NotNull Furnace furnace, boolean saveToFile) {
this.furnaceConfig.set("furnaces." + furnace.getUniqueID(), furnace);
public void saveFurnace(@NotNull Machine machine, boolean saveToFile) {
this.machineConfig.set("furnaces." + machine.getUniqueID(), machine);
if (saveToFile)
saveConfig();
}

/**
* Remove a furnace from YAML storage
* Remove a machine from YAML storage
* <p><b>NOTE:</b> If choosing not to save to file, this change will not take effect
* in the YAML file, this may be useful it removing a large batch and saving file at the
* in the YAML file, this may be useful if removing a large batch and saving file at the
* end of the batch change, use {@link #saveConfig()} to save all changes to file</p>
*
* @param furnace Furnace to remove
* @param machine Machine to remove
* @param saveToFile Whether to save changes to file
*/
public void removeFurnaceFromConfig(@NotNull Furnace furnace, boolean saveToFile) {
this.furnaceConfig.set("furnaces." + furnace.getUniqueID(), null);
public void removeFurnaceFromConfig(@NotNull Machine machine, boolean saveToFile) {
this.machineConfig.set("furnaces." + machine.getUniqueID(), null);
if (saveToFile)
saveConfig();
}

/**
* Remove a {@link Machine}
* <p>This will remove from memory and from file (if saving is true)
*
* @param machine Machine to remove
* @param saveToFile Whether to save to file
*/
public void removeMachine(@NotNull Machine machine, boolean saveToFile) {
this.machineMap.remove(machine.getUniqueID());
removeFurnaceFromConfig(machine, saveToFile);
}

/**
* Save all furnaces to file
*/
public void saveAll() {
for (Furnace furnace : this.furnaceMap.values()) {
saveFurnace(furnace, false);
for (Machine machine : this.machineMap.values()) {
saveFurnace(machine, false);
}
saveConfig();
}
Expand All @@ -341,7 +394,7 @@ public void saveAll() {
@SuppressWarnings("CallToPrintStackTrace")
public void saveConfig() {
try {
furnaceConfig.save(furnaceFile);
machineConfig.save(machineFile);
} catch (ConcurrentModificationException ignore) {
// TODO figure out a proper way to handle this exception and figure out why its happening
} catch (IOException e) {
Expand All @@ -351,7 +404,7 @@ public void saveConfig() {

void shutdown() {
saveAll();
furnaceMap.clear();
machineMap.clear();
}

}
20 changes: 10 additions & 10 deletions src/main/java/com/shanebeestudios/vf/api/VirtualFurnaceAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.shanebeestudios.vf.api.machine.Furnace;
import com.shanebeestudios.vf.api.property.FurnaceProperties;
import com.shanebeestudios.vf.api.task.FurnaceTick;
import com.shanebeestudios.vf.api.task.MachineTick;
import com.shanebeestudios.vf.api.task.TileTick;
import com.shanebeestudios.vf.api.tile.FurnaceTile;
import com.shanebeestudios.vf.api.util.Util;
Expand Down Expand Up @@ -34,7 +34,7 @@ public class VirtualFurnaceAPI {
private RecipeManager recipeManager;
private FurnaceManager furnaceManager;
private TileManager tileManager;
private FurnaceTick furnaceTick;
private MachineTick machineTick;
private TileTick tileTick;

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ public VirtualFurnaceAPI(@NotNull JavaPlugin javaPlugin, boolean disableMetrics)
this.recipeManager = null;
this.furnaceManager = null;
this.tileManager = null;
this.furnaceTick = null;
this.machineTick = null;
this.tileTick = null;
Util.error("&cFailed to initialize VirtualFurnaceAPI");
Util.error("&7 - Bukkit version: &b" + Bukkit.getBukkitVersion() + " &7is not supported!");
Expand All @@ -76,8 +76,8 @@ public VirtualFurnaceAPI(@NotNull JavaPlugin javaPlugin, boolean disableMetrics)
this.furnaceManager = new FurnaceManager(this);
this.tileManager = new TileManager(this);
this.tileManager.load();
this.furnaceTick = new FurnaceTick(this);
this.furnaceTick.start();
this.machineTick = new MachineTick(this);
this.machineTick.start();
this.tileTick = new TileTick(this);
this.tileTick.start();
Bukkit.getPluginManager().registerEvents(new FurnaceListener(this), javaPlugin);
Expand All @@ -90,9 +90,9 @@ public VirtualFurnaceAPI(@NotNull JavaPlugin javaPlugin, boolean disableMetrics)
* This should be used in a plugin's {@link Plugin#onDisable() onDisable()} method</p>
*/
public void disableAPI() {
this.furnaceTick.cancel();
this.machineTick.cancel();
this.tileTick.cancel();
this.furnaceTick = null;
this.machineTick = null;
this.tileTick = null;
this.furnaceManager.shutdown();
this.tileManager.shutdown();
Expand All @@ -107,7 +107,7 @@ public void disableAPI() {
* <p>This is good to use in your onDisable() method, to prevent tasks still running on server shutdown/reload</p>
*/
public void disableFurnaceTick() {
this.furnaceTick.cancel();
this.machineTick.cancel();
}

/**
Expand Down Expand Up @@ -161,8 +161,8 @@ public TileManager getTileManager() {
*
* @return Instance of furnace tick
*/
public FurnaceTick getFurnaceTick() {
return furnaceTick;
public MachineTick getFurnaceTick() {
return machineTick;
}

/**
Expand Down
Loading

0 comments on commit deecbbd

Please sign in to comment.