diff --git a/pom.xml b/pom.xml index 8c17d96..ce31e12 100644 --- a/pom.xml +++ b/pom.xml @@ -107,7 +107,7 @@ com.github.froxynetwork froxyapi - 0.0.3 + 0.0.4 diff --git a/src/main/java/com/froxynetwork/froxycore/FroxyCore.java b/src/main/java/com/froxynetwork/froxycore/FroxyCore.java index 4213149..b5cec78 100644 --- a/src/main/java/com/froxynetwork/froxycore/FroxyCore.java +++ b/src/main/java/com/froxynetwork/froxycore/FroxyCore.java @@ -8,9 +8,9 @@ import org.slf4j.LoggerFactory; import com.froxynetwork.froxyapi.Froxy; -import com.froxynetwork.froxyapi.language.LanguageManager; import com.froxynetwork.froxycore.api.APIImpl; import com.froxynetwork.froxycore.api.command.CommandManagerImpl; +import com.froxynetwork.froxycore.api.inventory.InventoryManagerImpl; import com.froxynetwork.froxycore.api.language.LanguageManagerImpl; /** @@ -50,11 +50,14 @@ public void onEnable() { // NetworkManager nm = new NetworkManager("http://localhost/", clientId, // clientSecret); - LanguageManager languageManager = new LanguageManagerImpl(); + LanguageManagerImpl languageManager = new LanguageManagerImpl(); CommandManagerImpl commandManager = new CommandManagerImpl(); + InventoryManagerImpl inventoryManager = new InventoryManagerImpl(); Bukkit.getPluginManager().registerEvents(commandManager, this); - APIImpl impl = new APIImpl(null, null, Constants.VERSION, log, languageManager, commandManager); + APIImpl impl = new APIImpl(this, null, Constants.VERSION, log, languageManager, commandManager, + inventoryManager); Froxy.setAPI(impl); + inventoryManager.init(); // TODO EDIT HERE File lang = new File("plugins" + File.separator + getDescription().getName() + File.separator + "lang"); Froxy.register(lang); diff --git a/src/main/java/com/froxynetwork/froxycore/api/APIImpl.java b/src/main/java/com/froxynetwork/froxycore/api/APIImpl.java index 68f3396..1720292 100644 --- a/src/main/java/com/froxynetwork/froxycore/api/APIImpl.java +++ b/src/main/java/com/froxynetwork/froxycore/api/APIImpl.java @@ -5,6 +5,7 @@ import com.froxynetwork.froxyapi.API; import com.froxynetwork.froxyapi.command.CommandManager; +import com.froxynetwork.froxyapi.inventory.InventoryManager; import com.froxynetwork.froxyapi.language.LanguageManager; /** @@ -47,14 +48,17 @@ public class APIImpl implements API { private CommandManager commandManager; + private InventoryManager inventoryManager; + public APIImpl(JavaPlugin corePlugin, JavaPlugin gamePlugin, String version, Logger logger, - LanguageManager languageManager, CommandManager commandManager) { + LanguageManager languageManager, CommandManager commandManager, InventoryManager inventoryManager) { this.corePlugin = corePlugin; this.gamePlugin = gamePlugin; this.version = version; this.logger = logger; this.languageManager = languageManager; this.commandManager = commandManager; + this.inventoryManager = inventoryManager; } @Override @@ -86,4 +90,9 @@ public LanguageManager getLanguageManager() { public CommandManager getCommandManager() { return commandManager; } + + @Override + public InventoryManager getInventoryManager() { + return inventoryManager; + } } diff --git a/src/main/java/com/froxynetwork/froxycore/api/inventory/InventoryImpl.java b/src/main/java/com/froxynetwork/froxycore/api/inventory/InventoryImpl.java new file mode 100644 index 0000000..7e7b926 --- /dev/null +++ b/src/main/java/com/froxynetwork/froxycore/api/inventory/InventoryImpl.java @@ -0,0 +1,115 @@ +package com.froxynetwork.froxycore.api.inventory; + +import java.util.HashMap; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; + +import com.froxynetwork.froxyapi.inventory.ClickableItem; +import com.froxynetwork.froxyapi.inventory.Inventory; +import com.froxynetwork.froxyapi.inventory.InventoryProvider; + +/** + * MIT License + * + * Copyright (c) 2019 FroxyNetwork + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @author 0ddlyoko + */ +public class InventoryImpl implements Inventory { + + private HashMap values; + private Player player; + private InventoryProvider inventoryProvider; + private int size; + private ClickableItem[] items; + private org.bukkit.inventory.Inventory bukkitInventory; + + public InventoryImpl(Player player, InventoryProvider inventoryProvider) { + this.values = new HashMap<>(); + this.player = player; + this.inventoryProvider = inventoryProvider; + this.size = inventoryProvider.rows(this); + this.items = new ClickableItem[9 * size]; + this.bukkitInventory = Bukkit.createInventory(player, size * 9, inventoryProvider.title(this)); + save(TICK, 0); + } + + @Override + public Player getPlayer() { + return player; + } + + @Override + public InventoryProvider getInventoryProvider() { + return inventoryProvider; + } + + public org.bukkit.inventory.Inventory getBukkitInventory() { + return bukkitInventory; + } + + @Override + public void set(int pos, ClickableItem item) { + if (pos < 0 || pos > size * 9) + throw new IllegalArgumentException("pos must be between 0 and " + (size * 9)); + items[pos] = item; + bukkitInventory.setItem(pos, item.getItem()); + } + + @Override + public void fill(ClickableItem item) { + for (int row = 0; row < size; row++) + for (int col = 0; col < 9; col++) + set(row * 9 + col, item); + } + + @Override + public void rectangle(int pos, int width, int height, ClickableItem item) { + // TODO + } + + public void open() { + player.openInventory(bukkitInventory); + } + + public void handler(InventoryClickEvent e) { + int pos = e.getSlot(); + if (pos < 0 || pos > items.length) + return; + ClickableItem item = items[pos]; + // Nothing to do + if (item == null) + return; + item.run(e); + } + + @Override + public void save(String key, Object value) { + values.put(key, value); + } + + @Override + public Object get(String key) { + return values.get(key); + } +} diff --git a/src/main/java/com/froxynetwork/froxycore/api/inventory/InventoryManagerImpl.java b/src/main/java/com/froxynetwork/froxycore/api/inventory/InventoryManagerImpl.java new file mode 100644 index 0000000..49b45f7 --- /dev/null +++ b/src/main/java/com/froxynetwork/froxycore/api/inventory/InventoryManagerImpl.java @@ -0,0 +1,120 @@ +package com.froxynetwork.froxycore.api.inventory; + +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; + +import com.froxynetwork.froxyapi.Froxy; +import com.froxynetwork.froxyapi.inventory.Inventory; +import com.froxynetwork.froxyapi.inventory.InventoryManager; +import com.froxynetwork.froxyapi.inventory.InventoryProvider; + +/** + * MIT License + * + * Copyright (c) 2019 FroxyNetwork + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @author 0ddlyoko + */ +public class InventoryManagerImpl implements InventoryManager, Listener { + private HashMap inventories; + + public InventoryManagerImpl() { + this.inventories = new HashMap<>(); + } + + public void init() { + Bukkit.getPluginManager().registerEvents(this, Froxy.getCorePlugin()); + Bukkit.getScheduler().scheduleSyncRepeatingTask(Froxy.getCorePlugin(), () -> { + if (inventories.size() == 0) + return; + for (Inventory inv : inventories.values()) { + int tick = 0; + Object currentTick = inv.get(Inventory.TICK); + if (currentTick != null && currentTick instanceof Integer) + tick = Integer.parseInt(currentTick.toString()); + inv.save(Inventory.TICK, tick + 1); + inv.getInventoryProvider().update(inv); + } + }, 1, 1); + } + + @Override + public Inventory openInventory(InventoryProvider provider, Player p) { + InventoryImpl inv = new InventoryImpl(p, provider); + inv.getInventoryProvider().init(inv); + inventories.put(p.getUniqueId(), inv); + inv.open(); + return inv; + } + + @Override + public InventoryImpl getInventory(Player p) { + return inventories.get(p.getUniqueId()); + } + + @Override + public boolean hasInventoryOpened(Player p) { + return inventories.containsKey(p.getUniqueId()); + } + + @EventHandler + public void onPlayerInventoryClick(InventoryClickEvent e) { + if (!inventories.containsKey(e.getWhoClicked().getUniqueId())) + return; + Player p = (Player) e.getWhoClicked(); + InventoryImpl inv = getInventory(p); + if (inv == null) { + // Impossible + return; + } + org.bukkit.inventory.Inventory clickedInventory = e.getClickedInventory(); + if (clickedInventory == null) + return; + e.setCancelled(true); + if (!inv.getBukkitInventory().equals(clickedInventory)) { + // The player doesn't click on the correct inventory + return; + } + inv.handler(e); + } + + @EventHandler + public void onPlayerInventoryDrag(InventoryDragEvent e) { + if (!inventories.containsKey(e.getWhoClicked().getUniqueId())) + return; + e.setCancelled(true); + } + + @EventHandler + public void onPlayerInventoryClose(InventoryCloseEvent e) { + if (!inventories.containsKey(e.getPlayer().getUniqueId())) + return; + inventories.remove(e.getPlayer().getUniqueId()); + } +}