Skip to content
This repository was archived by the owner on Nov 10, 2021. It is now read-only.

Commit c65fe44

Browse files
committed
Merge branch 'develop'
2 parents ad39656 + 50b1dce commit c65fe44

File tree

5 files changed

+252
-5
lines changed

5 files changed

+252
-5
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<dependency>
108108
<groupId>com.github.froxynetwork</groupId>
109109
<artifactId>froxyapi</artifactId>
110-
<version>0.0.3</version>
110+
<version>0.0.4</version>
111111
</dependency>
112112

113113
<!--Spigot API -->

src/main/java/com/froxynetwork/froxycore/FroxyCore.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import org.slf4j.LoggerFactory;
99

1010
import com.froxynetwork.froxyapi.Froxy;
11-
import com.froxynetwork.froxyapi.language.LanguageManager;
1211
import com.froxynetwork.froxycore.api.APIImpl;
1312
import com.froxynetwork.froxycore.api.command.CommandManagerImpl;
13+
import com.froxynetwork.froxycore.api.inventory.InventoryManagerImpl;
1414
import com.froxynetwork.froxycore.api.language.LanguageManagerImpl;
1515

1616
/**
@@ -50,11 +50,14 @@ public void onEnable() {
5050
// NetworkManager nm = new NetworkManager("http://localhost/", clientId,
5151
// clientSecret);
5252

53-
LanguageManager languageManager = new LanguageManagerImpl();
53+
LanguageManagerImpl languageManager = new LanguageManagerImpl();
5454
CommandManagerImpl commandManager = new CommandManagerImpl();
55+
InventoryManagerImpl inventoryManager = new InventoryManagerImpl();
5556
Bukkit.getPluginManager().registerEvents(commandManager, this);
56-
APIImpl impl = new APIImpl(null, null, Constants.VERSION, log, languageManager, commandManager);
57+
APIImpl impl = new APIImpl(this, null, Constants.VERSION, log, languageManager, commandManager,
58+
inventoryManager);
5759
Froxy.setAPI(impl);
60+
inventoryManager.init();
5861
// TODO EDIT HERE
5962
File lang = new File("plugins" + File.separator + getDescription().getName() + File.separator + "lang");
6063
Froxy.register(lang);

src/main/java/com/froxynetwork/froxycore/api/APIImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.froxynetwork.froxyapi.API;
77
import com.froxynetwork.froxyapi.command.CommandManager;
8+
import com.froxynetwork.froxyapi.inventory.InventoryManager;
89
import com.froxynetwork.froxyapi.language.LanguageManager;
910

1011
/**
@@ -47,14 +48,17 @@ public class APIImpl implements API {
4748

4849
private CommandManager commandManager;
4950

51+
private InventoryManager inventoryManager;
52+
5053
public APIImpl(JavaPlugin corePlugin, JavaPlugin gamePlugin, String version, Logger logger,
51-
LanguageManager languageManager, CommandManager commandManager) {
54+
LanguageManager languageManager, CommandManager commandManager, InventoryManager inventoryManager) {
5255
this.corePlugin = corePlugin;
5356
this.gamePlugin = gamePlugin;
5457
this.version = version;
5558
this.logger = logger;
5659
this.languageManager = languageManager;
5760
this.commandManager = commandManager;
61+
this.inventoryManager = inventoryManager;
5862
}
5963

6064
@Override
@@ -86,4 +90,9 @@ public LanguageManager getLanguageManager() {
8690
public CommandManager getCommandManager() {
8791
return commandManager;
8892
}
93+
94+
@Override
95+
public InventoryManager getInventoryManager() {
96+
return inventoryManager;
97+
}
8998
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.froxynetwork.froxycore.api.inventory;
2+
3+
import java.util.HashMap;
4+
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.entity.Player;
7+
import org.bukkit.event.inventory.InventoryClickEvent;
8+
9+
import com.froxynetwork.froxyapi.inventory.ClickableItem;
10+
import com.froxynetwork.froxyapi.inventory.Inventory;
11+
import com.froxynetwork.froxyapi.inventory.InventoryProvider;
12+
13+
/**
14+
* MIT License
15+
*
16+
* Copyright (c) 2019 FroxyNetwork
17+
*
18+
* Permission is hereby granted, free of charge, to any person obtaining a copy
19+
* of this software and associated documentation files (the "Software"), to deal
20+
* in the Software without restriction, including without limitation the rights
21+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22+
* copies of the Software, and to permit persons to whom the Software is
23+
* furnished to do so, subject to the following conditions:
24+
*
25+
* The above copyright notice and this permission notice shall be included in
26+
* all copies or substantial portions of the Software.
27+
*
28+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34+
* SOFTWARE.
35+
*
36+
* @author 0ddlyoko
37+
*/
38+
public class InventoryImpl implements Inventory {
39+
40+
private HashMap<String, Object> values;
41+
private Player player;
42+
private InventoryProvider inventoryProvider;
43+
private int size;
44+
private ClickableItem[] items;
45+
private org.bukkit.inventory.Inventory bukkitInventory;
46+
47+
public InventoryImpl(Player player, InventoryProvider inventoryProvider) {
48+
this.values = new HashMap<>();
49+
this.player = player;
50+
this.inventoryProvider = inventoryProvider;
51+
this.size = inventoryProvider.rows(this);
52+
this.items = new ClickableItem[9 * size];
53+
this.bukkitInventory = Bukkit.createInventory(player, size * 9, inventoryProvider.title(this));
54+
save(TICK, 0);
55+
}
56+
57+
@Override
58+
public Player getPlayer() {
59+
return player;
60+
}
61+
62+
@Override
63+
public InventoryProvider getInventoryProvider() {
64+
return inventoryProvider;
65+
}
66+
67+
public org.bukkit.inventory.Inventory getBukkitInventory() {
68+
return bukkitInventory;
69+
}
70+
71+
@Override
72+
public void set(int pos, ClickableItem item) {
73+
if (pos < 0 || pos > size * 9)
74+
throw new IllegalArgumentException("pos must be between 0 and " + (size * 9));
75+
items[pos] = item;
76+
bukkitInventory.setItem(pos, item.getItem());
77+
}
78+
79+
@Override
80+
public void fill(ClickableItem item) {
81+
for (int row = 0; row < size; row++)
82+
for (int col = 0; col < 9; col++)
83+
set(row * 9 + col, item);
84+
}
85+
86+
@Override
87+
public void rectangle(int pos, int width, int height, ClickableItem item) {
88+
// TODO
89+
}
90+
91+
public void open() {
92+
player.openInventory(bukkitInventory);
93+
}
94+
95+
public void handler(InventoryClickEvent e) {
96+
int pos = e.getSlot();
97+
if (pos < 0 || pos > items.length)
98+
return;
99+
ClickableItem item = items[pos];
100+
// Nothing to do
101+
if (item == null)
102+
return;
103+
item.run(e);
104+
}
105+
106+
@Override
107+
public void save(String key, Object value) {
108+
values.put(key, value);
109+
}
110+
111+
@Override
112+
public Object get(String key) {
113+
return values.get(key);
114+
}
115+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.froxynetwork.froxycore.api.inventory;
2+
3+
import java.util.HashMap;
4+
import java.util.UUID;
5+
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.event.EventHandler;
9+
import org.bukkit.event.Listener;
10+
import org.bukkit.event.inventory.InventoryClickEvent;
11+
import org.bukkit.event.inventory.InventoryCloseEvent;
12+
import org.bukkit.event.inventory.InventoryDragEvent;
13+
14+
import com.froxynetwork.froxyapi.Froxy;
15+
import com.froxynetwork.froxyapi.inventory.Inventory;
16+
import com.froxynetwork.froxyapi.inventory.InventoryManager;
17+
import com.froxynetwork.froxyapi.inventory.InventoryProvider;
18+
19+
/**
20+
* MIT License
21+
*
22+
* Copyright (c) 2019 FroxyNetwork
23+
*
24+
* Permission is hereby granted, free of charge, to any person obtaining a copy
25+
* of this software and associated documentation files (the "Software"), to deal
26+
* in the Software without restriction, including without limitation the rights
27+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28+
* copies of the Software, and to permit persons to whom the Software is
29+
* furnished to do so, subject to the following conditions:
30+
*
31+
* The above copyright notice and this permission notice shall be included in
32+
* all copies or substantial portions of the Software.
33+
*
34+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40+
* SOFTWARE.
41+
*
42+
* @author 0ddlyoko
43+
*/
44+
public class InventoryManagerImpl implements InventoryManager, Listener {
45+
private HashMap<UUID, InventoryImpl> inventories;
46+
47+
public InventoryManagerImpl() {
48+
this.inventories = new HashMap<>();
49+
}
50+
51+
public void init() {
52+
Bukkit.getPluginManager().registerEvents(this, Froxy.getCorePlugin());
53+
Bukkit.getScheduler().scheduleSyncRepeatingTask(Froxy.getCorePlugin(), () -> {
54+
if (inventories.size() == 0)
55+
return;
56+
for (Inventory inv : inventories.values()) {
57+
int tick = 0;
58+
Object currentTick = inv.get(Inventory.TICK);
59+
if (currentTick != null && currentTick instanceof Integer)
60+
tick = Integer.parseInt(currentTick.toString());
61+
inv.save(Inventory.TICK, tick + 1);
62+
inv.getInventoryProvider().update(inv);
63+
}
64+
}, 1, 1);
65+
}
66+
67+
@Override
68+
public Inventory openInventory(InventoryProvider provider, Player p) {
69+
InventoryImpl inv = new InventoryImpl(p, provider);
70+
inv.getInventoryProvider().init(inv);
71+
inventories.put(p.getUniqueId(), inv);
72+
inv.open();
73+
return inv;
74+
}
75+
76+
@Override
77+
public InventoryImpl getInventory(Player p) {
78+
return inventories.get(p.getUniqueId());
79+
}
80+
81+
@Override
82+
public boolean hasInventoryOpened(Player p) {
83+
return inventories.containsKey(p.getUniqueId());
84+
}
85+
86+
@EventHandler
87+
public void onPlayerInventoryClick(InventoryClickEvent e) {
88+
if (!inventories.containsKey(e.getWhoClicked().getUniqueId()))
89+
return;
90+
Player p = (Player) e.getWhoClicked();
91+
InventoryImpl inv = getInventory(p);
92+
if (inv == null) {
93+
// Impossible
94+
return;
95+
}
96+
org.bukkit.inventory.Inventory clickedInventory = e.getClickedInventory();
97+
if (clickedInventory == null)
98+
return;
99+
e.setCancelled(true);
100+
if (!inv.getBukkitInventory().equals(clickedInventory)) {
101+
// The player doesn't click on the correct inventory
102+
return;
103+
}
104+
inv.handler(e);
105+
}
106+
107+
@EventHandler
108+
public void onPlayerInventoryDrag(InventoryDragEvent e) {
109+
if (!inventories.containsKey(e.getWhoClicked().getUniqueId()))
110+
return;
111+
e.setCancelled(true);
112+
}
113+
114+
@EventHandler
115+
public void onPlayerInventoryClose(InventoryCloseEvent e) {
116+
if (!inventories.containsKey(e.getPlayer().getUniqueId()))
117+
return;
118+
inventories.remove(e.getPlayer().getUniqueId());
119+
}
120+
}

0 commit comments

Comments
 (0)