Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
efekos committed May 30, 2023
1 parent 4862238 commit f210a35
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 170 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![](https://jitpack.io/v/efe124/Simpler.svg)](https://jitpack.io/#efe124/Simpler)
[![License: ISC](https://img.shields.io/badge/License-ISC-yellow.svg)](https://opensource.org/licenses/MIT)
![version](https://img.shields.io/badge/version-1.0.9-blue)
![version](https://img.shields.io/badge/version-1.2.0-blue)
# Simpler

## Installation
Expand Down
62 changes: 0 additions & 62 deletions src/main/java/me/efekos/simpler/Simpler.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
package me.efekos.simpler;

import me.efekos.simpler.annotations.Register;
import me.efekos.simpler.commands.BaseCommand;
import me.efekos.simpler.commands.CommandManager;
import me.efekos.simpler.commands.CoreCommand;
import me.efekos.simpler.events.MenuEvents;
import me.efekos.simpler.events.PlayerEvents;
import me.efekos.simpler.exception.InvalidAnnotationException;
import me.efekos.simpler.items.CustomItem;
import me.efekos.simpler.items.ItemManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.InvalidClassException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public final class Simpler extends JavaPlugin {
@Override
public void onEnable() {
Expand All @@ -24,53 +11,4 @@ public void onEnable() {
public void onDisable() {
// Plugin shutdown logic
}

/**
* Set up the entire thing easily!
* @param plugin The plugin to use it with registering
* @throws InvalidAnnotationException If the annotations of a class is missing or wrong.
* @throws InvalidClassException If one of the classes annotated with @Register is wrong.
*/
@Deprecated
private static void setup(JavaPlugin plugin) throws InvalidAnnotationException,InvalidClassException {
Class<?>[] registers = Utils.getAllClassesAnnotatedWith(Register.class);

try{
for (Class<?> register:registers){
switch (register.getAnnotation(Register.class).value()){
case ITEM_TYPE:
if(register.isAssignableFrom(CustomItem.class)){
Class<? extends CustomItem> item = (Class<? extends CustomItem>) register;

Constructor<? extends CustomItem> itemConstructor = item.getConstructor();
itemConstructor.setAccessible(true);
CustomItem newItem = itemConstructor.newInstance();

ItemManager.registerItem(newItem.getId(), item);
} else {
throw new InvalidClassException(register.getName() + " Must extend me.efekos.simpler.items.CustomItem");
}
break;
case CORE_COMMAND:
if(register.isAssignableFrom(CoreCommand.class)){
Class<? extends CoreCommand> command = (Class<? extends CoreCommand>) register;
CommandManager.registerCoreCommand(plugin,command);
} else {
throw new InvalidClassException(register.getName() + " Must extend me.efekos.simpler.commands.CoreCommand");
}
break;
case BASE_COMMAND:
if(register.isAssignableFrom(BaseCommand.class)){
Class<? extends BaseCommand> command = (Class<? extends BaseCommand>) register;
CommandManager.registerBaseCommand(plugin,command);
} else {
throw new InvalidClassException(register.getName() + " Must extend me.efekos.simpler.commands.BaseCommand");
}
break;
}
}
}catch (NoSuchFieldException |IllegalAccessException |NoSuchMethodException|InvocationTargetException|InstantiationException e){
e.printStackTrace();
}
}
}
74 changes: 26 additions & 48 deletions src/main/java/me/efekos/simpler/events/PlayerEvents.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package me.efekos.simpler.events;

import me.efekos.simpler.items.CustomItem;
import me.efekos.simpler.items.CustomItemRegister;
import me.efekos.simpler.items.ItemManager;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
Expand All @@ -15,44 +13,33 @@
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.UUID;

public class PlayerEvents implements Listener {
@EventHandler
public void onPlayerInteract(PlayerInteractEvent e){
if(!e.hasItem()) return;
if(!e.getItem().hasItemMeta()) return;

try {
ItemStack stack = e.getItem();
ItemMeta meta = stack.getItemMeta();
PersistentDataContainer container = meta.getPersistentDataContainer();

if(!container.has(ItemManager.itemTypeKey, PersistentDataType.STRING))return;

ArrayList<CustomItemRegister> registers = ItemManager.getRegisters();
String id = container.get(ItemManager.itemTypeKey,PersistentDataType.STRING);
ItemStack stack = e.getItem();
ItemMeta meta = stack.getItemMeta();
PersistentDataContainer container = meta.getPersistentDataContainer();

if(registers==null)return;
CustomItemRegister register = (CustomItemRegister) registers.stream().filter(customItemRegister -> customItemRegister.getId().equals(id)).toArray()[0];
if(register==null)return;
Constructor<? extends CustomItem> constructor = register.getClazz().getConstructor();
constructor.setAccessible(true);
CustomItem item = constructor.newInstance();
String itemUuid = container.get(ItemManager.itemUuidKey,PersistentDataType.STRING);
if(itemUuid==null) return;
CustomItem item = ItemManager.getItems().get(UUID.fromString(itemUuid));
if(item==null)return;

switch (e.getAction()){
case LEFT_CLICK_AIR:case LEFT_CLICK_BLOCK:
item.onLeftClick(e);
break;
case RIGHT_CLICK_AIR:case RIGHT_CLICK_BLOCK:
item.onRightClick(e);
break;
default:
break;
}
} catch (Exception ex){
ex.printStackTrace();
switch (e.getAction()){
case LEFT_CLICK_AIR: case LEFT_CLICK_BLOCK:
item.onLeftClick(e);
break;
case RIGHT_CLICK_AIR: case RIGHT_CLICK_BLOCK:
item.onRightClick(e);
break;
default:
break;
}
}

Expand All @@ -64,15 +51,11 @@ public void onPlayerPickup(EntityPickupItemEvent e){
ItemMeta meta = stack.getItemMeta();
if(meta==null)return;
PersistentDataContainer container = meta.getPersistentDataContainer();
if(!container.has(ItemManager.itemTypeKey, PersistentDataType.STRING))return;
ArrayList<CustomItemRegister> registers = ItemManager.getRegisters();
String id = container.get(ItemManager.itemTypeKey,PersistentDataType.STRING);
if(registers==null)return;
CustomItemRegister register = (CustomItemRegister) registers.stream().filter(customItemRegister -> customItemRegister.getId().equals(id)).toArray()[0];
if(register==null)return;
Constructor<? extends CustomItem> constructor = register.getClazz().getConstructor();
constructor.setAccessible(true);
CustomItem item = constructor.newInstance();

String itemUuid = container.get(ItemManager.itemUuidKey,PersistentDataType.STRING);
if(itemUuid==null) return;
CustomItem item = ItemManager.getItems().get(UUID.fromString(itemUuid));
if(item==null)return;

item.onPickup(e);
} catch (Exception ex){
Expand All @@ -87,15 +70,10 @@ public void onPlayerDrop(PlayerDropItemEvent e){
ItemMeta meta = stack.getItemMeta();
if(meta==null)return;
PersistentDataContainer container = meta.getPersistentDataContainer();
if(!container.has(ItemManager.itemTypeKey, PersistentDataType.STRING))return;
ArrayList<CustomItemRegister> registers = ItemManager.getRegisters();
String id = container.get(ItemManager.itemTypeKey,PersistentDataType.STRING);
if(registers==null)return;
CustomItemRegister register = (CustomItemRegister) registers.stream().filter(customItemRegister -> customItemRegister.getId().equals(id)).toArray()[0];
if(register==null)return;
Constructor<? extends CustomItem> constructor = register.getClazz().getConstructor();
constructor.setAccessible(true);
CustomItem item = constructor.newInstance();
String itemUuid = container.get(ItemManager.itemUuidKey,PersistentDataType.STRING);
if(itemUuid==null) return;
CustomItem item = ItemManager.getItems().get(UUID.fromString(itemUuid));
if(item==null)return;

item.onDrop(e);
} catch (Exception ex){
Expand Down
41 changes: 6 additions & 35 deletions src/main/java/me/efekos/simpler/items/CustomItem.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package me.efekos.simpler.items;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
Expand All @@ -11,45 +9,18 @@

public abstract class CustomItem {

/**
* executes when a player left clicks with this item.
* @param player Player that left-clicked to the item.
*/
public abstract void onLeftClick(PlayerInteractEvent player);

/**
* executes when a player right clicks with this item.
* @param player Player that right-clicked to the item
*/
public abstract void onRightClick(PlayerInteractEvent player);

/**
* executes when a player drops this item.
* @param player Player that dropped the item
*/
public abstract void onDrop(PlayerDropItemEvent player);

/**
* executes when a player picks up this item.
* @param player Player that dropped this item
*/
public abstract void onPickup(EntityPickupItemEvent player);

/**
* @return An ID for this item. This ID is unique and used to know that an {@link org.bukkit.inventory.ItemStack} is this item.
*/
public abstract void onLeftClick(PlayerInteractEvent event);
public abstract void onRightClick(PlayerInteractEvent event);
public abstract void onDrop(PlayerDropItemEvent event);
public abstract void onPickup(EntityPickupItemEvent event);


@NotNull
public abstract String getId();

/**
* @return An ItemMeta to change the Instances of this item.
*/
@NotNull
public abstract ItemMeta getDefaultMeta();

/**
* @return A Material to use for the Instances of this item.
*/
@NotNull
public abstract Material getMaterial();

Expand Down
39 changes: 15 additions & 24 deletions src/main/java/me/efekos/simpler/items/ItemManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,25 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

public class ItemManager {
public static NamespacedKey itemTypeKey;
public static NamespacedKey itemUuidKey;

private static JavaPlugin plugin;
private static ArrayList<CustomItemRegister> registers = new ArrayList<>();
private static boolean isSetup;
private static HashMap<UUID,CustomItem> items = new HashMap<>();

public static HashMap<UUID, CustomItem> getItems() {
return items;
}

public static void setPlugin(JavaPlugin plugin) {
ItemManager.plugin = plugin;
itemTypeKey = new NamespacedKey(plugin,"item_id");
}

public static void registerItem(String name, Class<? extends CustomItem> itemClass){
try{
if(plugin==null) throw new NoPluginException("Call method me.efekos.simpler.items.ItemManager#setPlugin first.");

if(!isSetup){
plugin.getServer().getPluginManager().registerEvents(new PlayerEvents(),plugin);
isSetup = true;
}

registers.add(new CustomItemRegister()
.setId(name)
.setClazz(itemClass)
);
} catch (Exception e){
e.printStackTrace();
}
}

public static ArrayList<CustomItemRegister> getRegisters() {
return registers;
itemUuidKey = new NamespacedKey(plugin,"item_uuid");
}

public static void giveItem(@NotNull Player player, Material type){
Expand All @@ -64,6 +49,12 @@ public static void giveItem(@NotNull Player player, @NotNull CustomItem item){
ItemMeta meta = item.getDefaultMeta();
PersistentDataContainer container = meta.getPersistentDataContainer();
container.set(itemTypeKey, PersistentDataType.STRING,item.getId());
UUID itemId = UUID.randomUUID();

container.set(itemUuidKey,PersistentDataType.STRING,itemId.toString());
items.put(itemId,item);


stack.setItemMeta(meta);
player.getInventory().addItem(stack);
}
Expand Down

0 comments on commit f210a35

Please sign in to comment.