This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
252 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/com/froxynetwork/froxycore/api/inventory/InventoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> 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); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
src/main/java/com/froxynetwork/froxycore/api/inventory/InventoryManagerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UUID, InventoryImpl> 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()); | ||
} | ||
} |