Core/src/main/java/eu/univento/core/listeners/WorkbenchEvents.java

250 lines
11 KiB
Java

/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.core.listeners;
import eu.univento.core.Core;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Phloxz
* @version 1.0
*/
public class WorkbenchEvents implements Listener {
private static Map<Location, List<Player>> workbenchList = new HashMap<>();
private static Map<Location, ItemStack[]> workbenchInventory = new HashMap<>();
@EventHandler(ignoreCancelled = true)
public void onPlayerInteract(final PlayerInteractEvent e) {
final Player player = e.getPlayer();
if (e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock().getType() == Material.WORKBENCH) {
if (player.getOpenInventory() != null) {
player.getOpenInventory().close();
}
final Location location = e.getClickedBlock().getLocation();
if (workbenchList.containsKey(location)) {
new BukkitRunnable() {
public void run() {
Inventory inventory = null;
ItemStack[] items = null;
if (workbenchList.get(location).size() <= 1) {
items = workbenchInventory.get(location);
} else {
final Player randomPlayer = workbenchList.get(location).get(0);
inventory = randomPlayer.getOpenInventory().getTopInventory();
}
if (inventory != null && inventory.getType() == InventoryType.WORKBENCH) {
workbenchInventory.remove(location);
}
for (int i = 0; i <= 9; ++i) {
if (inventory != null) {
if (player.getOpenInventory() != null && player.getOpenInventory().getTopInventory() != null && player.getOpenInventory().getTopInventory().getType() == InventoryType.WORKBENCH) {
player.getOpenInventory().getTopInventory().setItem(i, inventory.getItem(i));
}
} else if (items != null && items.length > i && items[i] != null && player.getOpenInventory() != null && player.getOpenInventory().getTopInventory() != null && player.getOpenInventory().getTopInventory().getType() == InventoryType.WORKBENCH) {
player.getOpenInventory().getTopInventory().setItem(i, items[i]);
}
}
}
}.runTaskLater(Core.getInstance(), 2L);
workbenchList.get(location).add(player);
} else {
workbenchList.put(location, new ArrayList<>());
workbenchList.get(location).add(player);
}
}
}
@EventHandler
public void onBlockBreak(final BlockBreakEvent e) {
final Location location = e.getBlock().getLocation();
if (workbenchList.containsKey(location)) {
dropItems(location);
workbenchList.remove(location);
}
}
@EventHandler
public void onInventoryClick(final InventoryClickEvent e) {
if (e.getClickedInventory() == null) {
return;
}
if (e.getInventory() == null) {
return;
}
if (e.getClickedInventory().getType() == InventoryType.WORKBENCH && e.getSlot() >= 0 && e.getSlot() <= 9) {
final Player player = (Player) e.getWhoClicked();
Location location = null;
for (final Map.Entry<Location, List<Player>> list : workbenchList.entrySet()) {
for (final Player otherPlayer : list.getValue()) {
if (otherPlayer.getOpenInventory().getTopInventory().equals(e.getClickedInventory())) {
location = list.getKey();
}
}
}
if (location != null) {
for (final Player otherPlayer2 : workbenchList.get(location)) {
new BukkitRunnable() {
public void run() {
for (int i = 0; i <= 9; ++i) {
if (otherPlayer2 != player) {
otherPlayer2.getOpenInventory().getTopInventory().setItem(i, player.getOpenInventory().getTopInventory().getItem(i));
}
}
}
}.runTaskLater(Core.getInstance(), 2L);
}
}
}
}
@EventHandler
public void onInventoryDrag(final InventoryDragEvent e) {
if (e.getInventory() == null) {
return;
}
if (e.getInventory().getType() == InventoryType.WORKBENCH) {
final Player player = (Player) e.getWhoClicked();
Location location = null;
for (final Map.Entry<Location, List<Player>> list : workbenchList.entrySet()) {
for (final Player otherPlayer : list.getValue()) {
if (otherPlayer.getOpenInventory().getTopInventory().equals(e.getInventory())) {
location = list.getKey();
}
}
}
if (location != null) {
for (final Player otherPlayer2 : workbenchList.get(location)) {
new BukkitRunnable() {
public void run() {
for (int i = 0; i <= 9; ++i) {
if (otherPlayer2 != player) {
otherPlayer2.getOpenInventory().getTopInventory().setItem(i, player.getOpenInventory().getTopInventory().getItem(i));
}
}
}
}.runTaskLater(Core.getInstance(), 2L);
}
}
}
}
@EventHandler
public void onInventoryQuit(final InventoryCloseEvent e) {
Location location = null;
for (final Map.Entry<Location, List<Player>> list : workbenchList.entrySet()) {
for (final Player otherPlayer : list.getValue()) {
if (otherPlayer.getOpenInventory().getTopInventory().equals(e.getInventory())) {
location = list.getKey();
}
}
}
if (location != null) {
final Player player = (Player) e.getPlayer();
workbenchList.get(location).remove(player);
if (workbenchList.get(location).size() == 0) {
if (e.getInventory().getType() == InventoryType.WORKBENCH) {
workbenchInventory.put(location, e.getInventory().getContents());
e.getInventory().clear();
}
} else {
e.getInventory().clear();
}
}
}
@EventHandler
public void onPlayerQuit(final PlayerQuitEvent e) {
final Player player = e.getPlayer();
Location location = null;
for (final Map.Entry<Location, List<Player>> list : workbenchList.entrySet()) {
for (final Player otherPlayer : list.getValue()) {
if (otherPlayer.equals(player)) {
location = list.getKey();
}
}
}
if (location != null) {
workbenchList.get(location).remove(player);
if (workbenchList.get(location).size() == 0 && player.getOpenInventory() != null && player.getOpenInventory().getTopInventory() != null && player.getOpenInventory().getTopInventory().getType() == InventoryType.WORKBENCH) {
workbenchInventory.put(location, player.getOpenInventory().getTopInventory().getContents());
player.getOpenInventory().getTopInventory().clear();
}
}
}
@EventHandler
public void onPlayerKick(final PlayerKickEvent e) {
final Player player = e.getPlayer();
Location location = null;
for (final Map.Entry<Location, List<Player>> list : workbenchList.entrySet()) {
for (final Player otherPlayer : list.getValue()) {
if (otherPlayer.equals(player)) {
location = list.getKey();
}
}
}
if (location != null) {
workbenchList.get(location).remove(player);
if (workbenchList.get(location).size() == 0 && player.getOpenInventory() != null && player.getOpenInventory().getTopInventory() != null && player.getOpenInventory().getTopInventory().getType() == InventoryType.WORKBENCH) {
workbenchInventory.put(location, player.getOpenInventory().getTopInventory().getContents());
player.getOpenInventory().getTopInventory().clear();
}
}
}
private void dropItems(final Location loc) {
if (loc != null) {
if (workbenchInventory.containsKey(loc)) {
int id = 0;
for (final ItemStack item : workbenchInventory.get(loc)) {
if (item != null && item.getType() != Material.AIR && id > 0) {
loc.getWorld().dropItemNaturally(loc, item);
}
++id;
}
}
} else {
for (final Map.Entry<Location, List<Player>> entry : workbenchList.entrySet()) {
for (final Player player : entry.getValue()) {
if (player.getOpenInventory() != null && player.getOpenInventory().getTopInventory() != null && player.getOpenInventory().getTopInventory().getType() == InventoryType.WORKBENCH) {
player.closeInventory();
}
}
}
for (final Map.Entry<Location, ItemStack[]> entry2 : workbenchInventory.entrySet()) {
final Location location = entry2.getKey();
int id2 = 0;
for (final ItemStack item2 : entry2.getValue()) {
if (item2 != null && item2.getType() != Material.AIR && id2 > 0) {
location.getWorld().dropItemNaturally(location, item2);
}
++id2;
}
}
}
}
}