Furnaces can now change owners during server lifespan for Smelting XP

This commit is contained in:
nossr50 2020-07-13 13:16:47 -07:00
parent fdd951f1f1
commit f6c7049cac
2 changed files with 127 additions and 70 deletions

View File

@ -1,7 +1,9 @@
Version 2.1.134 Version 2.1.134
Minor code cleanup Smelting furnaces are now more flexible about who they consider their owner (see notes)
Smelting now has a Bonus Drops section in config.yml Smelting now has a Bonus Drops section in config.yml
Smelting now only doubles smelting results for items which have bonus drop entries in the config Smelting now only doubles smelting results for items which have bonus drop entries in config.yml
Minor code cleanup
Changed the UUID updater task to not catastrophically fail when requests failed
Fixed a bug where players could set each other on fire when partied or when PVP was disabled Fixed a bug where players could set each other on fire when partied or when PVP was disabled
Fixed a NPE that could happen with thrown potions Fixed a NPE that could happen with thrown potions
Fixed a potential NPE when damaging player armor with Axes Fixed a potential NPE when damaging player armor with Axes
@ -13,7 +15,16 @@ Version 2.1.134
Fixed a potential NPE when players right click blocks Fixed a potential NPE when players right click blocks
Fixed a locale mistake in locale hu_HU Fixed a locale mistake in locale hu_HU
Fixed a locale mistake in locale ru Fixed a locale mistake in locale ru
Changed the UUID updater task to not catastrophically fail when requests failed
NOTES:
Furnaces give XP to their owner while smelting
You become the owner of a Furnace by doing one of the following
1) Opening an empty furnace
2) Interacting with a furnace
It used to be that Furnaces would assign an owner and that would be their owner until the server shutdown, now owners will change based on who last had their hands on the furnace.
You won't become the owner if you are not allowed to view the inventory of a furnace
Version 2.1.133 Version 2.1.133
A fix for an 'array out of bounds' error related to players clicking outside the inventory windows has been fixed A fix for an 'array out of bounds' error related to players clicking outside the inventory windows has been fixed

View File

@ -29,6 +29,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.inventory.*; import org.bukkit.event.inventory.*;
import org.bukkit.inventory.*; import org.bukkit.inventory.*;
import org.bukkit.metadata.MetadataValue; import org.bukkit.metadata.MetadataValue;
import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.List;
@ -45,26 +46,57 @@ public class InventoryListener implements Listener {
if(WorldBlacklist.isWorldBlacklisted(event.getPlayer().getWorld())) if(WorldBlacklist.isWorldBlacklisted(event.getPlayer().getWorld()))
return; return;
Block furnaceBlock = processInventoryOpenOrCloseEvent(event.getInventory()); HumanEntity humanEntity = event.getPlayer();
Player player = (Player) humanEntity;
if (furnaceBlock == null) {
return;
}
HumanEntity player = event.getPlayer();
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
//Profile not loaded //Profile not loaded
if(UserManager.getPlayer((Player) player) == null) if(UserManager.getPlayer(player) == null)
{ {
return; return;
} }
if(!furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey) && furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey).size() == 0) if(event.getInventory() instanceof Furnace) {
furnaceBlock.setMetadata(mcMMO.furnaceMetadataKey, UserManager.getPlayer((Player) player).getPlayerMetadata());
}
Furnace furnace = getFurnace(event.getInventory());
if (furnace != null) {
if(isFurnaceAvailable(furnace, player)) {
assignFurnace(furnace, player);
}
}
}
public boolean isFurnaceAvailable(Furnace furnace, Player player) {
if(!furnace.hasMetadata(mcMMO.furnaceMetadataKey)
&& furnace.getMetadata(mcMMO.furnaceMetadataKey).size() == 0) {
return true;
} else {
if(player != getPlayerFromFurnace(furnace)) {
if(isFurnaceResultEmpty(furnace)) {
return true;
} else {
return false;
}
}
}
return false;
}
public boolean isFurnaceResultEmpty(Furnace furnace) {
return furnace.getInventory().getResult() == null;
}
public void assignFurnace(Furnace furnace, Player player) {
if(furnace.hasMetadata(mcMMO.furnaceMetadataKey)) {
furnace.removeMetadata(mcMMO.furnaceMetadataKey, mcMMO.p);
}
furnace.setMetadata(mcMMO.furnaceMetadataKey, UserManager.getPlayer(player).getPlayerMetadata());
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@ -73,19 +105,15 @@ public class InventoryListener implements Listener {
if(WorldBlacklist.isWorldBlacklisted(event.getPlayer().getWorld())) if(WorldBlacklist.isWorldBlacklisted(event.getPlayer().getWorld()))
return; return;
Block furnaceBlock = processInventoryOpenOrCloseEvent(event.getInventory()); if(event.getInventory() instanceof FurnaceInventory) {
if(getFurnace(event.getInventory()) != null) {
Furnace furnace = getFurnace(event.getInventory());
if (furnaceBlock == null || furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) { if(isFurnaceOwned(furnace) && isFurnaceResultEmpty(furnace)) {
return; removeFurnaceOwner(furnace);
}
} }
HumanEntity player = event.getPlayer();
if (!UserManager.hasPlayerDataKey(player)) {
return;
} }
furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, plugin);
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@ -102,7 +130,9 @@ public class InventoryListener implements Listener {
return; return;
} }
Player player = getPlayerFromFurnace(furnaceBlock); Furnace furnace = (Furnace) furnaceState;
Player player = getPlayerFromFurnace(furnace);
/* WORLD GUARD MAIN FLAG CHECK */ /* WORLD GUARD MAIN FLAG CHECK */
if(WorldGuardUtils.isWorldGuardLoaded()) if(WorldGuardUtils.isWorldGuardLoaded())
@ -137,7 +167,9 @@ public class InventoryListener implements Listener {
return; return;
} }
Player player = getPlayerFromFurnace(furnaceBlock); if(furnaceBlock instanceof Furnace)
{
Player player = getPlayerFromFurnace((Furnace) furnaceBlock);
/* WORLD GUARD MAIN FLAG CHECK */ /* WORLD GUARD MAIN FLAG CHECK */
if(WorldGuardUtils.isWorldGuardLoaded()) if(WorldGuardUtils.isWorldGuardLoaded())
@ -158,6 +190,7 @@ public class InventoryListener implements Listener {
event.setResult(UserManager.getPlayer(player).getSmeltingManager().smeltProcessing(smelting, event.getResult())); event.setResult(UserManager.getPlayer(player).getSmeltingManager().smeltProcessing(smelting, event.getResult()));
} }
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onFurnaceExtractEvent(FurnaceExtractEvent event) { public void onFurnaceExtractEvent(FurnaceExtractEvent event) {
@ -171,7 +204,9 @@ public class InventoryListener implements Listener {
return; return;
} }
Player player = getPlayerFromFurnace(furnaceBlock); if(furnaceBlock instanceof Furnace) {
Furnace furnace = (Furnace) furnaceBlock;
Player player = getPlayerFromFurnace(furnace);
/* WORLD GUARD MAIN FLAG CHECK */ /* WORLD GUARD MAIN FLAG CHECK */
if(WorldGuardUtils.isWorldGuardLoaded()) if(WorldGuardUtils.isWorldGuardLoaded())
@ -194,6 +229,7 @@ public class InventoryListener implements Listener {
int exp = UserManager.getPlayer(player).getSmeltingManager().vanillaXPBoost(xpToDrop); int exp = UserManager.getPlayer(player).getSmeltingManager().vanillaXPBoost(xpToDrop);
event.setExpToDrop(exp); event.setExpToDrop(exp);
} }
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onInventoryClickEventNormal(InventoryClickEvent event) { public void onInventoryClickEventNormal(InventoryClickEvent event) {
@ -206,12 +242,13 @@ public class InventoryListener implements Listener {
if(event.getWhoClicked() instanceof Player) if(event.getWhoClicked() instanceof Player)
{ {
Player player = ((Player) event.getWhoClicked()).getPlayer(); Player player = ((Player) event.getWhoClicked()).getPlayer();
Block furnaceBlock = processInventoryOpenOrCloseEvent(event.getInventory()); Furnace furnace = getFurnace(event.getInventory());
if (furnaceBlock != null) if (furnace != null)
{ {
if (furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey).size() > 0) if (isFurnaceOwned(furnace)) {
furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, mcMMO.p); removeFurnaceOwner(furnace);
}
//Profile not loaded //Profile not loaded
if(UserManager.getPlayer(player) == null) if(UserManager.getPlayer(player) == null)
@ -219,7 +256,7 @@ public class InventoryListener implements Listener {
return; return;
} }
furnaceBlock.setMetadata(mcMMO.furnaceMetadataKey, UserManager.getPlayer(player).getPlayerMetadata()); assignFurnace(furnace, player);
} }
} }
@ -325,6 +362,14 @@ public class InventoryListener implements Listener {
} }
} }
public boolean isFurnaceOwned(Furnace furnace) {
return furnace.getMetadata(mcMMO.furnaceMetadataKey).size() > 0;
}
public void removeFurnaceOwner(Furnace furnace) {
furnace.removeMetadata(mcMMO.furnaceMetadataKey, mcMMO.p);
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onInventoryDragEvent(InventoryDragEvent event) { public void onInventoryDragEvent(InventoryDragEvent event) {
/* WORLD BLACKLIST CHECK */ /* WORLD BLACKLIST CHECK */
@ -480,7 +525,7 @@ public class InventoryListener implements Listener {
new PlayerUpdateInventoryTask((Player) whoClicked).runTaskLater(plugin, 0); new PlayerUpdateInventoryTask((Player) whoClicked).runTaskLater(plugin, 0);
} }
private Block processInventoryOpenOrCloseEvent(Inventory inventory) { private Furnace getFurnace(Inventory inventory) {
if (!(inventory instanceof FurnaceInventory)) { if (!(inventory instanceof FurnaceInventory)) {
return null; return null;
} }
@ -491,11 +536,12 @@ public class InventoryListener implements Listener {
return null; return null;
} }
return furnace.getBlock(); return furnace;
} }
private Player getPlayerFromFurnace(Block furnaceBlock) { @Nullable
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey); private Player getPlayerFromFurnace(Furnace furnace) {
List<MetadataValue> metadata = furnace.getMetadata(mcMMO.furnaceMetadataKey);
if (metadata.isEmpty()) { if (metadata.isEmpty()) {
return null; return null;