Archived
0

Convert to maven

This commit is contained in:
Dico200
2017-11-15 18:32:49 +00:00
parent ea8751f65e
commit be8175a5bc
6 changed files with 375 additions and 327 deletions

8
.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
### IntelliJ specific files
*.iml
.idea/
### Maven
target/

42
pom.xml Normal file
View File

@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.redstoner</groupId>
<artifactId>ChestAPI</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<!--Spigot API-->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--Bukkit API-->
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,21 +1,21 @@
package de.pepich.chestapi; package de.pepich.chestapi;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class ChestAPI public class ChestAPI
{ {
private static JavaPlugin plugin = null; private static JavaPlugin plugin = null;
public static void init(JavaPlugin plugin) public static void init(JavaPlugin plugin)
{ {
ChestAPI.plugin = plugin; ChestAPI.plugin = plugin;
} }
public static JavaPlugin getPlugin() public static JavaPlugin getPlugin()
{ {
if (plugin == null) if (plugin == null)
plugin = (JavaPlugin) Bukkit.getPluginManager().getPlugins()[0]; plugin = (JavaPlugin) Bukkit.getPluginManager().getPlugins()[0];
return plugin; return plugin;
} }
} }

View File

@@ -1,306 +1,304 @@
package de.pepich.chestapi; package de.pepich.chestapi;
import java.util.HashMap; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import javax.annotation.Nullable; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.Bukkit; import org.bukkit.event.inventory.ClickType;
import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.EventHandler; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.Inventory;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.inventory.ItemStack;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory; import java.util.HashMap;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; public class ClickableInventory implements Listener
{
public class ClickableInventory implements Listener private int open = 0;
{ private final String title;
private int open = 0; private DefaultSize size;
private final String title; private HashMap<Integer, CallbackHandler> handlers = new HashMap<Integer, CallbackHandler>();
private DefaultSize size; private CallbackHandler background;
private HashMap<Integer, CallbackHandler> handlers = new HashMap<Integer, CallbackHandler>(); private CallbackHandler fallback;
private CallbackHandler background; private CallbackHandler close;
private CallbackHandler fallback;
private CallbackHandler close; private Inventory inventory;
private final InventoryHolder holder;
private Inventory inventory;
private final InventoryHolder holder; /** Creates a new ClickableInventory with the given name.</br>
* The ClickableInventory will be created with a DefaultSize as specified by {@link DefaultSize#DYNAMIC_AUTO(int, int) DYNAMIC_AUTO(9, 54)}.
/** Creates a new ClickableInventory with the given name.</br> *
* The ClickableInventory will be created with a DefaultSize as specified by {@link DefaultSize#DYNAMIC_AUTO(int, int) DYNAMIC_AUTO(9, 54)}. * @param title The name to be displayed. */
* public ClickableInventory(final String title)
* @param name The name to be displayed. */ {
public ClickableInventory(final String title) this(title, DefaultSize.DYNAMIC_AUTO(9, 54));
{ }
this(title, DefaultSize.DYNAMIC_AUTO(9, 54));
} /** Creates a new ClickableInventory with the given name and the specified DefaultSize.
*
/** Creates a new ClickableInventory with the given name and the specified DefaultSize. * @param title The name to be displayed.
* * @param size The DefaultSize for the inventory to be used. */
* @param name The name to be displayed. public ClickableInventory(final String title, final DefaultSize size)
* @param size The DefaultSize for the inventory to be used. */ {
public ClickableInventory(final String title, final DefaultSize size) this.size = size;
{ this.title = title;
this.size = size; holder = new CustomHolder(this);
this.title = title; if (size.allowSquareShape() && size.getPreferredSize() == 9)
holder = new CustomHolder(this); inventory = Bukkit.createInventory(holder, InventoryType.DROPPER, title);
if (size.allowSquareShape() && size.getPreferredSize() == 9) else
inventory = Bukkit.createInventory(holder, InventoryType.DROPPER, title); inventory = Bukkit.createInventory(holder, size.getPreferredSize(), title);
else }
inventory = Bukkit.createInventory(holder, size.getPreferredSize(), title);
} /** Displays the inventory.
*
/** Displays the inventory. * @param player The player that is going to see the inventory. */
* public void show(Player player)
* @param player The player that is going to see the inventory. */ {
public void show(Player player) player.openInventory(inventory);
{ if (open == 0)
player.openInventory(inventory); Bukkit.getPluginManager().registerEvents(this, ChestAPI.getPlugin());
if (open == 0) open++;
Bukkit.getPluginManager().registerEvents(this, ChestAPI.getPlugin()); }
open++;
} // ------------------------------------------------------------- //
// -------------------- GETTERS AND SETTERS -------------------- //
// ------------------------------------------------------------- // // ------------------------------------------------------------- //
// -------------------- GETTERS AND SETTERS -------------------- // // If you want docs on these, go fork off. Getters/Setters. Duh. //
// ------------------------------------------------------------- //
// If you want docs on these, go fork off. Getters/Setters. Duh. // public String getName()
{
public String getName() return title;
{ }
return title;
} public int getFirstFree()
{
public int getFirstFree() if (handlers.size() == inventory.getSize())
{ return -1;
if (handlers.size() == inventory.getSize()) for (int i = 1; i <= inventory.getSize(); i++)
return -1; if (!handlers.containsKey(i))
for (int i = 1; i <= inventory.getSize(); i++) return i;
if (!handlers.containsKey(i)) return -1;
return i; }
return -1;
} public int getLastFree()
{
public int getLastFree() if (handlers.size() == inventory.getSize())
{ return -1;
if (handlers.size() == inventory.getSize()) for (int i = inventory.getSize(); i > 0; i--)
return -1; if (!handlers.containsKey(i))
for (int i = inventory.getSize(); i > 0; i--) return i;
if (!handlers.containsKey(i)) return -1;
return i; }
return -1;
} public int getFirstUsed()
{
public int getFirstUsed() if (handlers.size() == inventory.getSize())
{ return -1;
if (handlers.size() == inventory.getSize()) for (int i = 1; i <= inventory.getSize(); i++)
return -1; if (handlers.containsKey(i))
for (int i = 1; i <= inventory.getSize(); i++) return i;
if (handlers.containsKey(i)) return -1;
return i; }
return -1;
} public int getLastUsed()
{
public int getLastUsed() if (handlers.size() == inventory.getSize())
{ return -1;
if (handlers.size() == inventory.getSize()) for (int i = inventory.getSize(); i > 0; i--)
return -1; if (handlers.containsKey(i))
for (int i = inventory.getSize(); i > 0; i--) return i;
if (handlers.containsKey(i)) return -1;
return i; }
return -1;
} protected Inventory getInventory()
{
protected Inventory getInventory() return inventory;
{ }
return inventory;
} // ------------------------------------------------------------- //
// ----------------------- SETUP METHODS ----------------------- //
// ------------------------------------------------------------- // // ------------------------------------------------------------- //
// ----------------------- SETUP METHODS ----------------------- //
// ------------------------------------------------------------- // /** Assigns an item and a handler to the given item slot.<br/>
* Setting a handler to null will NOT remove it, but it will flag the slot as "used", meaning that it will be skipped when searching for the first/last free spot.
/** Assigns an item and a handler to the given item slot.<br/> *
* Setting a handler to null will NOT remove it, but it will flag the slot as "used", meaning that it will be skipped when searching for the first/last free spot. * @param slot An integer value representing the slot for the ItemStack. Must be any of -998, [-3:max_length].<br/>
* * The value -998 represents background clicks.<br/>
* @param slot An integer value representing the slot for the ItemStack. Must be any of -998, [-3:max_length].<br/> * The value -3 represents the "close handler", which will be fired when the player closes the inventory.<br/>
* The value -998 represents background clicks.<br/> * The value -2 represents the "fallback handler", which will be used if no appropriate handler could be found.<br/>
* The value -3 represents the "close handler", which will be fired when the player closes the inventory.<br/> * The value -1 represents the last free slot, 0 points to the first free slot. If an actual number is given, the specified slot will be overwritten.<br/>
* The value -2 represents the "fallback handler", which will be used if no appropriate handler could be found.<br/> * The first slot is numbered with 1, as opposed to starting at 0, due to the additional meaning that 0 has.
* The value -1 represents the last free slot, 0 points to the first free slot. If an actual number is given, the specified slot will be overwritten.<br/> * @param item An ItemStack that will be displayed. Set to null if you want to assign functionality to an empty slot.
* The first slot is numbered with 1, as opposed to starting at 0, due to the additional meaning that 0 has. * @param handler A CallbackHandler which will be notified on click. When the handler is null then the handler will be removed, but the item will still be assigned.
* @param item An ItemStack that will be displayed. Set to null if you want to assign functionality to an empty slot. * @throws IllegalArgumentException when the location does not exist or when one of -1 or 0 are specified and the inventory is already full. */
* @param handler A CallbackHandler which will be notified on click. When the handler is null then the handler will be removed, but the item will still be assigned. public void set(int slot, final ItemStack item, final CallbackHandler handler)
* @throws IllegalArgumentException when the location does not exist or when one of -1 or 0 are specified and the inventory is already full. */ {
public void set(int slot, @Nullable final ItemStack item, @Nullable final CallbackHandler handler) if (slot > inventory.getSize())
{ resize(slot);
if (slot > inventory.getSize()) if (slot < -1 || slot > size.getMaxSize())
resize(slot); throw new IllegalArgumentException(
if (slot < -1 || slot > size.getMaxSize()) "The given slot (" + slot + ") is out of bounds (-1, 0, [1:" + size.getMaxSize() + "])");
throw new IllegalArgumentException( else
"The given slot (" + slot + ") is out of bounds (-1, 0, [1:" + size.getMaxSize() + "])"); {
else if (slot == -998)
{ background = handler;
if (slot == -998) else if (slot == -2)
background = handler; fallback = handler;
else if (slot == -2) else if (slot == -3)
fallback = handler; close = handler;
else if (slot == -3) else
close = handler; {
else if (slot == 0)
{ slot = getFirstFree();
if (slot == 0) else if (slot == -1)
slot = getFirstFree(); slot = getLastFree();
else if (slot == -1) if (slot == -1)
slot = getLastFree(); throw new IllegalArgumentException("No free spot could be found!");
if (slot == -1)
throw new IllegalArgumentException("No free spot could be found!"); if (item == null)
inventory.remove(inventory.getItem(slot - 1));
if (item == null) else
inventory.remove(inventory.getItem(slot - 1)); inventory.setItem(slot - 1, item);
else
inventory.setItem(slot - 1, item); handlers.put(slot, handler);
}
handlers.put(slot, handler); }
} if (size.doAutoResize())
} resize();
if (size.doAutoResize()) }
resize();
} /** Removes an assigned action from the inventory. Most be any of -1, 0, [1:max_length].<br/>
* Calling this method will free up a slot again so that it can be filled up by using the "first/last free" selector.
/** Removes an assigned action from the inventory. Most be any of -1, 0, [1:max_length].<br/> *
* Calling this method will free up a slot again so that it can be filled up by using the "first/last free" selector. * @param slot An integer value representing the slot for the ItemStack. Must be any of -998, [-3:max_length].<br/>
* * The value -998 represents background clicks.<br/>
* @param slot An integer value representing the slot for the ItemStack. Must be any of -998, [-3:max_length].<br/> * The value -3 represents the "close handler", which will be fired when the player closes the inventory.<br/>
* The value -998 represents background clicks.<br/> * The value -2 represents the "fallback handler", which will be used if no appropriate handler could be found.<br/>
* The value -3 represents the "close handler", which will be fired when the player closes the inventory.<br/> * The value -1 represents the last used slot, 0 points to the first used slot. If an actual number is given, the specified slot will be overwritten.<br/>
* The value -2 represents the "fallback handler", which will be used if no appropriate handler could be found.<br/> * The first slot is numbered with 1, as opposed to starting at 0, due to the additional meaning that 0 has.
* The value -1 represents the last used slot, 0 points to the first used slot. If an actual number is given, the specified slot will be overwritten.<br/> * @throws IllegalArgumentException when the location does not exist. */
* The first slot is numbered with 1, as opposed to starting at 0, due to the additional meaning that 0 has. public void remove(int slot)
* @throws IllegalArgumentException when the location does not exist. */ {
public void remove(int slot) if (slot == -998)
{ background = null;
if (slot == -998) else if (slot == -2)
background = null; fallback = null;
else if (slot == -2) else if (slot == -3)
fallback = null; close = null;
else if (slot == -3) else
close = null; {
else if (slot < -1 || slot > size.getMaxSize())
{ throw new IllegalArgumentException(
if (slot < -1 || slot > size.getMaxSize()) "The given slot (" + slot + ") is out of bounds (-1, 0, [1:" + size.getMaxSize() + "])");
throw new IllegalArgumentException( if (slot == 0)
"The given slot (" + slot + ") is out of bounds (-1, 0, [1:" + size.getMaxSize() + "])"); slot = getFirstUsed();
if (slot == 0) else if (slot == -1)
slot = getFirstUsed(); slot = getLastUsed();
else if (slot == -1) if (slot == -1)
slot = getLastUsed(); throw new IllegalArgumentException("No free spot could be found!");
if (slot == -1) handlers.remove(slot);
throw new IllegalArgumentException("No free spot could be found!"); inventory.setItem(slot - 1, null);
handlers.remove(slot); if (size.doAutoResize())
inventory.setItem(slot - 1, null); resize();
if (size.doAutoResize()) }
resize(); }
}
} /** This method removes all TRAILING empty lines.<br/>
* This method will utilize hopper and dropper inventories if possible. Dropper inventories can be disabled by selection a RECTANGLE size constraint.<br/>
/** This method removes all TRAILING empty lines.<br/> * Can not be used on final sized inventories.
* This method will utilize hopper and dropper inventories if possible. Dropper inventories can be disabled by selection a RECTANGLE size constraint.<br/> *
* Can not be used on final sized inventories. * @return the new size.
* * @throws IllegalAccessError if the Inventories size is final. */
* @return the new size. public int resize()
* @throws IllegalAccessError if the Inventories size is final. */ {
public int resize() if (size.isFinalSize())
{ throw new IllegalAccessError("Can not resize an inventory with a FINAL size constraint.");
if (size.isFinalSize()) final int target_size = getLastUsed();
throw new IllegalAccessError("Can not resize an inventory with a FINAL size constraint."); Inventory new_inventory;
final int target_size = getLastUsed(); if (target_size <= 5)
Inventory new_inventory; new_inventory = Bukkit.createInventory(holder, InventoryType.HOPPER, title);
if (target_size <= 5) else if (target_size <= 9 && size.allowSquareShape())
new_inventory = Bukkit.createInventory(holder, InventoryType.HOPPER, title); new_inventory = Bukkit.createInventory(holder, InventoryType.DROPPER, title);
else if (target_size <= 9 && size.allowSquareShape()) else
new_inventory = Bukkit.createInventory(holder, InventoryType.DROPPER, title); new_inventory = Bukkit.createInventory(holder, target_size + ((9 - (target_size % 9)) % 9), title);
else for (int i = 0; i < new_inventory.getSize() && i < inventory.getSize(); i++)
new_inventory = Bukkit.createInventory(holder, target_size + ((9 - (target_size % 9)) % 9), title); new_inventory.setItem(i, inventory.getItem(i));
for (int i = 0; i < new_inventory.getSize() && i < inventory.getSize(); i++) inventory = new_inventory;
new_inventory.setItem(i, inventory.getItem(i)); return inventory.getSize();
inventory = new_inventory; }
return inventory.getSize();
} /** This method removes all TRAILING empty lines.<br/>
* This method will utilize hopper and dropper inventories if possible. Dropper inventories can be disabled by selection a RECTANGLE size constraint.<br/>
/** This method removes all TRAILING empty lines.<br/> * Can not be used on final sized inventories.
* This method will utilize hopper and dropper inventories if possible. Dropper inventories can be disabled by selection a RECTANGLE size constraint.<br/> *
* Can not be used on final sized inventories. * @return the new size.
* * @throws IllegalAccessError if the Inventories size is final. */
* @return the new size. private void resize(int target_size)
* @throws IllegalAccessError if the Inventories size is final. */ {
private void resize(int target_size) if (size.isFinalSize())
{ throw new IllegalAccessError("Can not resize an inventory with a FINAL size constraint.");
if (size.isFinalSize()) target_size = Math.max(target_size, getLastUsed());
throw new IllegalAccessError("Can not resize an inventory with a FINAL size constraint."); Inventory new_inventory;
target_size = Math.max(target_size, getLastUsed()); if (target_size <= 5)
Inventory new_inventory; new_inventory = Bukkit.createInventory(holder, InventoryType.HOPPER, title);
if (target_size <= 5) else if (target_size <= 9 && size.allowSquareShape())
new_inventory = Bukkit.createInventory(holder, InventoryType.HOPPER, title); new_inventory = Bukkit.createInventory(holder, InventoryType.DROPPER, title);
else if (target_size <= 9 && size.allowSquareShape()) else
new_inventory = Bukkit.createInventory(holder, InventoryType.DROPPER, title); new_inventory = Bukkit.createInventory(holder, target_size + ((9 - (target_size % 9)) % 9), title);
else for (int i = 0; i < new_inventory.getSize() && i < inventory.getSize(); i++)
new_inventory = Bukkit.createInventory(holder, target_size + ((9 - (target_size % 9)) % 9), title); new_inventory.setItem(i, inventory.getItem(i));
for (int i = 0; i < new_inventory.getSize() && i < inventory.getSize(); i++) inventory = new_inventory;
new_inventory.setItem(i, inventory.getItem(i)); }
inventory = new_inventory;
} // ------------------------------------------------------------- //
// ------------------------- LISTENERS ------------------------- //
// ------------------------------------------------------------- // // ------------------------------------------------------------- //
// ------------------------- LISTENERS ------------------------- //
// ------------------------------------------------------------- // @EventHandler
public void onInventoryClose(InventoryCloseEvent event)
@EventHandler {
public void onInventoryClose(InventoryCloseEvent event) if (this.inventory.equals(event.getInventory()))
{ {
if (this.inventory.equals(event.getInventory())) open--;
{ if (open == 0)
open--; event.getHandlers().unregister(this);
if (open == 0) if (close != null)
event.getHandlers().unregister(this); close.run((Player) event.getPlayer(), ClickType.UNKNOWN);
if (close != null) }
close.run((Player) event.getPlayer(), ClickType.UNKNOWN); }
}
} @EventHandler
public void onInventoryClick(InventoryClickEvent event)
@EventHandler {
public void onInventoryClick(InventoryClickEvent event) if (this.inventory.equals(event.getInventory()))
{ {
if (this.inventory.equals(event.getInventory())) event.setCancelled(true);
{ final int slot = event.getSlot() + 1;
event.setCancelled(true); CallbackHandler handler;
final int slot = event.getSlot() + 1; if (slot == -998)
CallbackHandler handler; handler = background;
if (slot == -998) else
handler = background; handler = handlers.get(slot);
else if ((handler = (handler == null ? fallback : handler)) != null)
handler = handlers.get(slot); handler.run((Player) event.getWhoClicked(), event.getClick());
if ((handler = (handler == null ? fallback : handler)) != null) }
handler.run((Player) event.getWhoClicked(), event.getClick()); }
} }
}
} class CustomHolder implements InventoryHolder
{
class CustomHolder implements InventoryHolder ClickableInventory inv;
{
ClickableInventory inv; protected CustomHolder(ClickableInventory inv)
{
protected CustomHolder(ClickableInventory inv) this.inv = inv;
{ }
this.inv = inv;
} @Override
public Inventory getInventory()
@Override {
public Inventory getInventory() return inv.getInventory();
{ }
return inv.getInventory(); }
}
}