Skip to content

Commit

Permalink
Machine - now implements InventoryHolder
Browse files Browse the repository at this point in the history
- This is something Bukkit doesn't allow, but Paper does
  • Loading branch information
ShaneBeee committed Feb 4, 2024
1 parent 9d130f3 commit 310c611
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 63 deletions.
20 changes: 2 additions & 18 deletions src/main/java/com/shanebeestudios/vf/api/FurnaceListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 All @@ -14,25 +13,19 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;

class FurnaceListener implements Listener {

private final FurnaceManager furnaceManager;
private final RecipeManager recipeManager;
private final TileManager tileManager;

private final Map<HumanEntity, Machine> openInventories = new HashMap<>();

FurnaceListener(VirtualFurnaceAPI virtualFurnaceAPI) {
this.furnaceManager = virtualFurnaceAPI.getFurnaceManager();
this.recipeManager = virtualFurnaceAPI.getRecipeManager();
Expand All @@ -51,7 +44,6 @@ private void onClickFurnace(PlayerInteractEvent event) {
if (furnace != null) {
event.setCancelled(true);
furnace.openInventory(player);
openInventories.put(player, furnace);
return;
}
}
Expand All @@ -66,32 +58,24 @@ private void onClickFurnace(PlayerInteractEvent event) {
event.setCancelled(true);
if (event.getHand() != EquipmentSlot.OFF_HAND) {
tile.activate(player);
openInventories.put(player, tile.getMachine());
}
}
}
}

@EventHandler
private void onCloseInventory(InventoryCloseEvent event) {
HumanEntity player = event.getPlayer();
openInventories.remove(player);

}

@EventHandler
private void onInventoryClick(InventoryClickEvent event) {
HumanEntity clicker = event.getWhoClicked();
if (openInventories.containsKey(clicker) && clicker instanceof Player) {
Furnace furnace = (Furnace) openInventories.get(clicker); // TODO This may change in the future
if (event.getInventory().getHolder() instanceof Furnace furnace && clicker instanceof Player player) {
int slot = event.getRawSlot();
// Give XP to player when they extract from the furnace
if (slot == 2) {
ItemStack output = furnace.getOutput();
if (output != null) {
int exp = (int) furnace.extractExperience();
// Call the furnace extract event
FurnaceExtractEvent extractEvent = new FurnaceExtractEvent(furnace, ((Player) clicker), output, exp);
FurnaceExtractEvent extractEvent = new FurnaceExtractEvent(furnace, player, output, exp);
extractEvent.callEvent();

((Player) clicker).giveExp(extractEvent.getExperience());
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/com/shanebeestudios/vf/api/FurnaceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Furnace createFurnace(@NotNull String name) {
* Create a new furnace
* <p>This will create a new furnace, add it to the tick list, and save to file</p>
*
* @param name Name of new furnace (This shows up in the inventory view)
* @param name Name of new furnace (This shows up in the inventory view)
* @param furnaceProperties Properties to apply to this furnace
* @return Instance of this new furnace
*/
Expand All @@ -106,9 +106,9 @@ public Furnace createFurnace(@NotNull String name, @NotNull Consumer<Furnace> fu
* Create a new furnace
* <p>This will create a new furnace, add it to the tick list, and save to file</p>
*
* @param name Name of new furnace (This shows up in the inventory view)
* @param name Name of new furnace (This shows up in the inventory view)
* @param furnaceProperties Properties to apply to this furnace
* @param function Function to run before furnace is created
* @param function Function to run before furnace is created
* @return Instance of this new furnace
*/
public Furnace createFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @Nullable Consumer<Furnace> function) {
Expand Down Expand Up @@ -137,10 +137,10 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull Material m
/**
* Create a {@link Furnace} that is attached to an {@link ItemStack}
*
* @param name Name of furnace (this will show up in the furnace UI)
* @param name Name of furnace (this will show up in the furnace UI)
* @param furnaceProperties Properties associated with this furnace item
* @param material Material of the new ItemStack
* @param glowing Whether the item should glow (enchanted)
* @param material Material of the new ItemStack
* @param glowing Whether the item should glow (enchanted)
* @return New ItemStack with a furnace attached
*/
public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @NotNull Material material, boolean glowing) {
Expand All @@ -164,11 +164,11 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull Material m
/**
* Create a {@link Furnace} that is attached to an {@link ItemStack}
*
* @param name Name of furnace (this will show up in the furnace UI)
* @param name Name of furnace (this will show up in the furnace UI)
* @param furnaceProperties Properties associated with this furnace item
* @param material Material of the new ItemStack
* @param glowing Whether the item should glow (enchanted)
* @param function Function to run before furnace is created
* @param material Material of the new ItemStack
* @param glowing Whether the item should glow (enchanted)
* @param function Function to run before furnace is created
* @return New ItemStack with a furnace attached
*/
public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @NotNull Material material, boolean glowing, @Nullable Consumer<Furnace> function) {
Expand All @@ -190,10 +190,10 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull ItemStack
/**
* Create a {@link Furnace} that is attached to an {@link ItemStack}
*
* @param name Name of furnace (this will show up in the furnace UI)
* @param name Name of furnace (this will show up in the furnace UI)
* @param furnaceProperties Properties associated with this furnace item
* @param itemStack ItemStack to be copied and have a furnace attached
* @param glowing Whether the item should glow (enchanted)
* @param itemStack ItemStack to be copied and have a furnace attached
* @param glowing Whether the item should glow (enchanted)
* @return Clone of the input ItemStack with a furnace attached
*/
public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @NotNull ItemStack itemStack, boolean glowing) {
Expand All @@ -217,11 +217,11 @@ public ItemStack createItemWithFurnace(@NotNull String name, @NotNull ItemStack
/**
* Create a {@link Furnace} that is attached to an {@link ItemStack}
*
* @param name Name of furnace (this will show up in the furnace UI)
* @param name Name of furnace (this will show up in the furnace UI)
* @param furnaceProperties Properties associated with this furnace item
* @param itemStack ItemStack to be copied and have a furnace attached
* @param glowing Whether the item should glow (enchanted)
* @param function Function to run before furnace is created
* @param itemStack ItemStack to be copied and have a furnace attached
* @param glowing Whether the item should glow (enchanted)
* @param function Function to run before furnace is created
* @return Clone of the input ItemStack with a furnace attached
*/
public ItemStack createItemWithFurnace(@NotNull String name, @NotNull FurnaceProperties furnaceProperties, @NotNull ItemStack itemStack, boolean glowing, @Nullable Consumer<Furnace> function) {
Expand Down Expand Up @@ -278,8 +278,7 @@ void loadFurnaces() {
ConfigurationSection section = this.furnaceConfig.getConfigurationSection("furnaces");
if (section != null) {
for (String string : section.getKeys(true)) {
if (section.get(string) instanceof Furnace) {
Furnace furnace = ((Furnace) section.get(string));
if (section.get(string) instanceof Furnace furnace) {
this.furnaceMap.put(UUID.fromString(string), (Furnace) section.get(string));
}
}
Expand Down Expand Up @@ -330,6 +329,7 @@ public void saveAll() {
/**
* Save current furnace YAML from RAM to file
*/
@SuppressWarnings("CallToPrintStackTrace")
public void saveConfig() {
try {
furnaceConfig.save(furnaceFile);
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/shanebeestudios/vf/api/event/Event.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.shanebeestudios.vf.api.event.machine;

import com.shanebeestudios.vf.api.event.Event;
import com.shanebeestudios.vf.api.machine.Furnace;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/shanebeestudios/vf/api/machine/Furnace.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public Furnace(String name) {
* @param name Name of the object which will show up in the UI
* @param furnaceProperties Property for this furnace
*/
@SuppressWarnings("deprecation") // Paper deprecation for AdventureAPI
public Furnace(String name, FurnaceProperties furnaceProperties) {
super(UUID.randomUUID(), name);
this.furnaceProperties = furnaceProperties;
Expand All @@ -74,12 +75,13 @@ public Furnace(String name, FurnaceProperties furnaceProperties) {
this.fuel = null;
this.input = null;
this.output = null;
this.inventory = Bukkit.createInventory(null, InventoryType.FURNACE, Util.getColString(name));
this.inventory = Bukkit.createInventory(this, InventoryType.FURNACE, Util.getColString(name));
this.experience = 0.0f;
this.updateInventory();
}

// Used for deserializer
@SuppressWarnings("deprecation") // Paper deprecation for AdventureAPI
private Furnace(String name, UUID uuid, int cookTime, int fuelTime, float xp, ItemStack fuel, ItemStack input, ItemStack output, FurnaceProperties furnaceProperties) {
super(uuid, name);
this.recipeManager = VirtualFurnaceAPI.getInstance().getRecipeManager();
Expand All @@ -103,7 +105,7 @@ private Furnace(String name, UUID uuid, int cookTime, int fuelTime, float xp, It
this.fuelTimeTotal = 0;
}
this.experience = xp;
this.inventory = Bukkit.createInventory(null, InventoryType.FURNACE, Util.getColString(name));
this.inventory = Bukkit.createInventory(this, InventoryType.FURNACE, Util.getColString(name));
this.updateInventory();
}

Expand Down Expand Up @@ -262,7 +264,7 @@ private void processBurn() {
Fuel fuel = this.recipeManager.getFuelByMaterial(this.fuel.getType());
if (fuel == null) return;
FurnaceFuelBurnEvent event = new FurnaceFuelBurnEvent(this, this.fuel, fuel, fuel.getBurnTime());
Bukkit.getPluginManager().callEvent(event);
event.callEvent();
if (event.isCancelled()) {
return;
}
Expand Down Expand Up @@ -305,7 +307,7 @@ private void processCook() {
this.experience += result.getExperience();

FurnaceCookEvent event = new FurnaceCookEvent(this, this.input, out);
Bukkit.getPluginManager().callEvent(event);
event.callEvent();
if (event.isCancelled()) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/shanebeestudios/vf/api/machine/Machine.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.shanebeestudios.vf.api.machine;

import com.shanebeestudios.vf.api.util.Util;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;

import java.util.Objects;
import java.util.UUID;
Expand All @@ -10,7 +10,7 @@
* Abstract machine class
* <p>Other machines will extend from this class</p>
*/
public abstract class Machine {
public abstract class Machine implements InventoryHolder {

private final String name;
private final UUID uniqueID;
Expand Down

0 comments on commit 310c611

Please sign in to comment.