sorted listeners properly

This commit is contained in:
Brianna O'Keefe 2018-11-25 16:28:58 -05:00
parent 24addc076a
commit 32237003c5
4 changed files with 94 additions and 60 deletions

View File

@ -200,6 +200,8 @@ public class UltimateStacker extends JavaPlugin {
Bukkit.getPluginManager().registerEvents(new DeathListeners(this), this);
Bukkit.getPluginManager().registerEvents(new ShearListeners(this), this);
Bukkit.getPluginManager().registerEvents(new InteractListeners(this), this);
Bukkit.getPluginManager().registerEvents(new InventoryListeners(this), this);
Bukkit.getPluginManager().registerEvents(new EntityListeners(this), this);
Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, 6000, 6000);

View File

@ -0,0 +1,47 @@
package com.songoda.ultimatestacker.events;
import com.songoda.ultimatestacker.UltimateStacker;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
public class EntityListeners implements Listener {
private final UltimateStacker instance;
public EntityListeners(UltimateStacker instance) {
this.instance = instance;
}
@EventHandler
public void onPickup(PlayerPickupItemEvent event) {
event.getItem().setItemStack(instance.getStackingTask().setMax(event.getItem().getItemStack(), 0, true));
ItemStack item = event.getItem().getItemStack();
int amt = item.getAmount();
int max = item.getMaxStackSize();
if (amt <= max) return;
event.setCancelled(true);
item.setAmount(max);
amt = amt - max;
while (amt > max) {
ItemStack newItem = new ItemStack(item);
newItem.setAmount(max);
event.getItem().getWorld().dropItemNaturally(event.getItem().getLocation(), newItem);
amt = amt - max;
}
ItemStack newItem = new ItemStack(item);
newItem.setAmount(amt);
event.getItem().getWorld().dropItemNaturally(event.getItem().getLocation(), newItem);
}
}

View File

@ -24,66 +24,6 @@ public class InteractListeners implements Listener {
this.instance = instance;
}
@EventHandler
public void onAccept(InventoryPickupItemEvent event) {
ItemStack item = event.getItem().getItemStack();
if (new ItemStack(item.getType()).getMaxStackSize() == item.getMaxStackSize()) return;
instance.getStackingTask().setMax(item, 0, true);
int amt = item.getAmount();
int max = item.getMaxStackSize();
if (amt <= max) return;
item.setAmount(max);
amt = amt - max;
while (amt > max) {
ItemStack newItem = new ItemStack(item);
newItem.setAmount(max);
event.getInventory().addItem(newItem);
amt = amt - max;
}
ItemStack newItem = new ItemStack(item);
newItem.setAmount(amt);
event.getInventory().addItem(newItem);
}
@EventHandler
public void onPickup(PlayerPickupItemEvent event) {
event.getItem().setItemStack(instance.getStackingTask().setMax(event.getItem().getItemStack(), 0, true));
ItemStack item = event.getItem().getItemStack();
int amt = item.getAmount();
int max = item.getMaxStackSize();
if (amt <= max) return;
event.setCancelled(true);
item.setAmount(max);
amt = amt - max;
while (amt > max) {
ItemStack newItem = new ItemStack(item);
newItem.setAmount(max);
event.getItem().getWorld().dropItemNaturally(event.getItem().getLocation(), newItem);
amt = amt - max;
}
ItemStack newItem = new ItemStack(item);
newItem.setAmount(amt);
event.getItem().getWorld().dropItemNaturally(event.getItem().getLocation(), newItem);
}
@EventHandler
public void onInteract(PlayerInteractAtEntityEvent event) {
Player player = event.getPlayer();

View File

@ -0,0 +1,45 @@
package com.songoda.ultimatestacker.events;
import com.songoda.ultimatestacker.UltimateStacker;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.inventory.ItemStack;
public class InventoryListeners implements Listener {
private final UltimateStacker instance;
public InventoryListeners(UltimateStacker instance) {
this.instance = instance;
}
@EventHandler
public void onAccept(InventoryPickupItemEvent event) {
ItemStack item = event.getItem().getItemStack();
if (new ItemStack(item.getType()).getMaxStackSize() == item.getMaxStackSize()) return;
instance.getStackingTask().setMax(item, 0, true);
int amt = item.getAmount();
int max = item.getMaxStackSize();
if (amt <= max) return;
item.setAmount(max);
amt = amt - max;
while (amt > max) {
ItemStack newItem = new ItemStack(item);
newItem.setAmount(max);
event.getInventory().addItem(newItem);
amt = amt - max;
}
ItemStack newItem = new ItemStack(item);
newItem.setAmount(amt);
event.getInventory().addItem(newItem);
}
}