Skip to content

Commit

Permalink
1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
efekos committed Jun 4, 2023
1 parent f210a35 commit 3a1fce2
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependency>
<groupId>com.github.efe124</groupId>
<artifactId>Simpler</artifactId>
<version>1.0.9</version>
<version>1.2.2</version>
</dependency>
````

Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.efekos</groupId>
<artifactId>Simpler</artifactId>
<version>1.0.9</version>
<version>1.2.2</version>
<packaging>jar</packaging>

<name>Simpler</name>
Expand Down Expand Up @@ -94,5 +94,10 @@
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
</project>
13 changes: 13 additions & 0 deletions src/main/java/me/efekos/simpler/Simpler.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package me.efekos.simpler;

import me.efekos.simpler.commands.CommandManager;
import me.efekos.simpler.egs.commands.GiveCookied;
import me.efekos.simpler.items.ItemManager;
import org.bukkit.plugin.java.JavaPlugin;

public final class Simpler extends JavaPlugin {
private final String customItemDataPath = getDataFolder().getAbsolutePath()+"\\CustomItemData.json";

@Override
public void onEnable() {
try {
CommandManager.registerBaseCommand(this, GiveCookied.class);
ItemManager.setPlugin(this);
ItemManager.loadItemData(customItemDataPath);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onDisable() {
// Plugin shutdown logic
ItemManager.saveItemData(customItemDataPath);
}
}
38 changes: 38 additions & 0 deletions src/main/java/me/efekos/simpler/egs/commands/GiveCookied.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.efekos.simpler.egs.commands;

import me.efekos.simpler.annotations.Command;
import me.efekos.simpler.commands.BaseCommand;
import me.efekos.simpler.commands.syntax.Syntax;
import me.efekos.simpler.egs.items.ClickerCokkie;
import me.efekos.simpler.items.ItemManager;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;

@Command(name = "givecookie",description = "Give you cookied")
public class GiveCookied extends BaseCommand {
@Override
public @NotNull Syntax getSyntax() {
return new Syntax();
}

public GiveCookied(@NotNull String name) {
super(name);
}

public GiveCookied(@NotNull String name, @NotNull String description, @NotNull String usageMessage, @NotNull List<String> aliases) {
super(name, description, usageMessage, aliases);
}

@Override
public void onPlayerUse(Player player, String[] args) {
ItemManager.giveItem(player,new ClickerCokkie());
}

@Override
public void onConsoleUse(ConsoleCommandSender sender, String[] args) {

}
}
50 changes: 50 additions & 0 deletions src/main/java/me/efekos/simpler/egs/items/ClickerCokkie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.efekos.simpler.egs.items;

import me.efekos.simpler.items.CustomItem;
import org.bukkit.Material;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;

public class ClickerCokkie extends CustomItem {
private int click;

@Override
public void onLeftClick(PlayerInteractEvent event) {
click +=1;
event.getPlayer().sendMessage("You clicked to this "+ click + " times.");
}

@Override
public void onRightClick(PlayerInteractEvent event) {
onLeftClick(event);
}

@Override
public void onDrop(PlayerDropItemEvent event) {

}

@Override
public void onPickup(EntityPickupItemEvent event) {

}

@Override
public @NotNull String getId() {
return "clicker_cookie";
}

@Override
public @NotNull ItemMeta getDefaultMeta() {
return new ItemStack(getMaterial()).getItemMeta();
}

@Override
public @NotNull Material getMaterial() {
return Material.COOKIE;
}
}
18 changes: 17 additions & 1 deletion src/main/java/me/efekos/simpler/items/CustomItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public abstract class CustomItem {

protected UUID _itemUuid;

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();

@NotNull
public UUID getUniqueItemId(){
return _itemUuid;
}

/**
* DO NOT USE THIS METHOD FOR ANY PURPOSE. CHANGING AN ITEM'S ID AT THE INSTANCE WILL %100 BOOST UP YOUR CHANCE AT GETTING ERRORS OUT OF NOWHERE. THIS METHOD IS ONLY MADE TO BE USED BY {@link ItemManager} CLASS.
* @param id new value
*/
public void setUniqueItemId(UUID id){
this._itemUuid = id;
}

@NotNull
public abstract ItemMeta getDefaultMeta();

Expand Down
24 changes: 0 additions & 24 deletions src/main/java/me/efekos/simpler/items/CustomItemRegister.java

This file was deleted.

84 changes: 71 additions & 13 deletions src/main/java/me/efekos/simpler/items/ItemManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.efekos.simpler.items;

import me.efekos.simpler.Simpler;
import com.google.gson.Gson;
import jdk.internal.access.foreign.MemorySegmentProxy;
import me.efekos.simpler.events.PlayerEvents;
import me.efekos.simpler.exception.NoPluginException;
import org.bukkit.Material;
Expand All @@ -15,6 +16,11 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;

Expand All @@ -26,6 +32,45 @@ public class ItemManager {
private static boolean isSetup;
private static HashMap<UUID,CustomItem> items = new HashMap<>();

public static void saveItemData(String path){
try {
if(!isSetup){
throw new NoPluginException("Call me.efekos.simpler.items.ItemManager.setPlugin(org.bukkit.plugin.java.JavaPlugin) first.");
}

Gson gson = new Gson();
File file = new File(path);
file.getParentFile().mkdir();
file.createNewFile();

Writer writer = new FileWriter(path,false);
gson.toJson(items,writer);
writer.flush();
writer.close();
} catch (Exception e){
e.printStackTrace();
}
}

public static void loadItemData(String path){
try {
if(!isSetup){
throw new NoPluginException("Call me.efekos.simpler.items.ItemManager.setPlugin(org.bukkit.plugin.java.JavaPlugin) first.");
}

Gson gson = new Gson();
File file = new File(path);
if(file.exists()){
Reader reader = new FileReader(file);
HashMap<UUID,CustomItem> itemMap = gson.fromJson(reader,HashMap.class);
items = itemMap;
}
} catch (Exception e){
e.printStackTrace();
}
System.out.println(items);
}

public static HashMap<UUID, CustomItem> getItems() {
return items;
}
Expand All @@ -34,6 +79,10 @@ public static void setPlugin(JavaPlugin plugin) {
ItemManager.plugin = plugin;
itemTypeKey = new NamespacedKey(plugin,"item_id");
itemUuidKey = new NamespacedKey(plugin,"item_uuid");

plugin.getServer().getPluginManager().registerEvents(new PlayerEvents(),plugin);

isSetup = true;
}

public static void giveItem(@NotNull Player player, Material type){
Expand All @@ -45,18 +94,27 @@ public static void giveItem(@NotNull Player player, ItemStack item){
}

public static void giveItem(@NotNull Player player, @NotNull CustomItem item){
ItemStack stack = new ItemStack(item.getMaterial());
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);
try{
if(!isSetup){
throw new NoPluginException("Call me.efekos.simpler.items.ItemManager.setPlugin(org.bukkit.plugin.java.JavaPlugin) first.");
} else {
ItemStack stack = new ItemStack(item.getMaterial());
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());
item.setUniqueItemId(itemId);
items.put(itemId,item);


stack.setItemMeta(meta);
player.getInventory().addItem(stack);
}
} catch (Exception e){
e.printStackTrace();
}
}

public static void giveSkull(@NotNull Player player, OfflinePlayer skullOwner){
Expand Down

0 comments on commit 3a1fce2

Please sign in to comment.