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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
0ddlyoko committed Jun 3, 2019
2 parents ad39656 + 50b1dce commit c65fe44
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<dependency>
<groupId>com.github.froxynetwork</groupId>
<artifactId>froxyapi</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>
</dependency>

<!--Spigot API -->
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/froxynetwork/froxycore/FroxyCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/froxynetwork/froxycore/api/APIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -86,4 +90,9 @@ public LanguageManager getLanguageManager() {
public CommandManager getCommandManager() {
return commandManager;
}

@Override
public InventoryManager getInventoryManager() {
return inventoryManager;
}
}
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);
}
}
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());
}
}

0 comments on commit c65fe44

Please sign in to comment.