Compare commits
No commits in common. "aceee4a8bc026cc7160bc240d68c338e913a1f1b" and "f347c014268e192e226995037a54cb1a76870846" have entirely different histories.
aceee4a8bc
...
f347c01426
@ -10,7 +10,6 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.jetbrains:annotations:24.0.0'
|
|
||||||
testImplementation platform('org.junit:junit-bom:5.9.1')
|
testImplementation platform('org.junit:junit-bom:5.9.1')
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
implementation(
|
implementation(
|
||||||
|
@ -1,49 +1,36 @@
|
|||||||
package tokarotik.giftapi;
|
package tokarotik.giftapi;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import tokarotik.giftapi.NBT.cache.CacheManager;
|
|
||||||
import tokarotik.giftapi.dev.GiftCommand;
|
import tokarotik.giftapi.dev.GiftCommand;
|
||||||
|
|
||||||
public class Main extends JavaPlugin implements Listener
|
public class Main extends JavaPlugin implements Listener
|
||||||
{
|
{
|
||||||
private CacheManager cacheManager;
|
|
||||||
private APIManager apiManager;
|
private APIManager apiManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable()
|
public void onEnable() {
|
||||||
{
|
|
||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
|
|
||||||
cacheManager = new CacheManager(54);
|
getLogger().info("GiftAPI enabled!");
|
||||||
|
|
||||||
|
saveDefaultConfig();
|
||||||
apiManager = new APIManager(this, getConfig());
|
apiManager = new APIManager(this, getConfig());
|
||||||
|
|
||||||
this.getCommand("gift").setExecutor(new GiftCommand(apiManager)); // DELETE FOR RELEASE
|
if (getConfig().getBoolean(ConfigPaths.DEVTEST, false))
|
||||||
|
{
|
||||||
getServer().getPluginManager().registerEvents(this, this); // bullshit
|
this.getCommand("gift").setExecutor(new GiftCommand(apiManager));
|
||||||
|
}
|
||||||
getLogger().info("GiftAPI enabled!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable()
|
public void onDisable() {
|
||||||
{
|
|
||||||
cacheManager.disable();
|
|
||||||
|
|
||||||
getLogger().info("GiftAPI disabled!");
|
getLogger().info("GiftAPI disabled!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onPlayerQuit(PlayerQuitEvent event)
|
|
||||||
{
|
|
||||||
cacheManager.playerQuit(event.getPlayer().getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
// if failed to add, return false
|
// if failed to add, return false
|
||||||
// if successfully added, return true
|
// if successfully added, return true
|
||||||
public boolean add(Player player, ItemStack item)
|
public boolean add(Player player, ItemStack item)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package tokarotik.giftapi.NBT.cache;
|
package tokarotik.giftapi.NBT;
|
||||||
|
|
||||||
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
||||||
import net.minecraft.server.v1_6_R3.NBTCompressedStreamTools;
|
import net.minecraft.server.v1_6_R3.NBTCompressedStreamTools;
|
||||||
@ -58,7 +58,7 @@ public class BasicNBT {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static EntityPlayer getPlayer(Player player)
|
public EntityPlayer getPlayer(Player player)
|
||||||
{
|
{
|
||||||
return ((CraftPlayer) player).getHandle();
|
return ((CraftPlayer) player).getHandle();
|
||||||
}
|
}
|
36
src/main/java/tokarotik/giftapi/NBT/NBTManager.java
Normal file
36
src/main/java/tokarotik/giftapi/NBT/NBTManager.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package tokarotik.giftapi.NBT;
|
||||||
|
|
||||||
|
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
||||||
|
import net.minecraft.server.v1_6_R3.NBTTagCompound;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
public class NBTManager
|
||||||
|
{
|
||||||
|
private final BasicNBT basicNBT;
|
||||||
|
|
||||||
|
public NBTManager(String world)
|
||||||
|
{
|
||||||
|
this.basicNBT = new BasicNBT(world);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean write(Player player)
|
||||||
|
{
|
||||||
|
EntityPlayer entityPlayer = this.basicNBT.getPlayer(player);
|
||||||
|
NBTTagCompound tag = this.basicNBT.readPlayerNBT(entityPlayer);
|
||||||
|
|
||||||
|
// some action
|
||||||
|
|
||||||
|
return this.basicNBT.writePlayerNBT(tag, entityPlayer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean read(Player player)
|
||||||
|
{
|
||||||
|
EntityPlayer entityPlayer = this.basicNBT.getPlayer(player);
|
||||||
|
NBTTagCompound tag = this.basicNBT.readPlayerNBT(entityPlayer);
|
||||||
|
|
||||||
|
//some action
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package tokarotik.giftapi.NBT;
|
||||||
|
|
||||||
|
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
||||||
|
import net.minecraft.server.v1_6_R3.NBTTagCompound;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
public class WorldNBTSynchronizer
|
||||||
|
{
|
||||||
|
public static String getWorld(EntityPlayer entityPlayer, String worldName)
|
||||||
|
{
|
||||||
|
BasicNBT basicNBT = new BasicNBT(worldName);
|
||||||
|
|
||||||
|
return worldName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasPlayer(String playerName, String worldName)
|
||||||
|
{
|
||||||
|
World world = Bukkit.getWorld(worldName);
|
||||||
|
|
||||||
|
for (Player player : world.getPlayers())
|
||||||
|
{
|
||||||
|
if (player.getName() == playerName) { return true; }
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasGiftApiNBT(EntityPlayer entityPlayer, String worldName)
|
||||||
|
{
|
||||||
|
BasicNBT basicNBT = new BasicNBT(worldName);
|
||||||
|
|
||||||
|
NBTTagCompound tag = basicNBT.readPlayerNBT(entityPlayer);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,92 +0,0 @@
|
|||||||
package tokarotik.giftapi.NBT.cache;
|
|
||||||
|
|
||||||
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
|
||||||
import net.minecraft.server.v1_6_R3.NBTTagCompound;
|
|
||||||
import net.minecraft.server.v1_6_R3.NBTTagList;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import tokarotik.giftapi.NBT.page.PageManager;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class CacheManager
|
|
||||||
{
|
|
||||||
private final Map<Player, PageManager> world_map = new HashMap<>();
|
|
||||||
private final Map<Player, PageManager> nether_map = new HashMap<>();
|
|
||||||
private final Map<Player, PageManager> end_map = new HashMap<>();
|
|
||||||
|
|
||||||
private final int max_stack;
|
|
||||||
|
|
||||||
public CacheManager(int max_stack)
|
|
||||||
{
|
|
||||||
this.max_stack = max_stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PageManager load(Player player)
|
|
||||||
{
|
|
||||||
String worldPlayer = player.getWorld().getName();
|
|
||||||
Map<Player, PageManager> hashMap = getWorlMap(worldPlayer);
|
|
||||||
EntityPlayer entityPlayer = BasicNBT.getPlayer(player);
|
|
||||||
|
|
||||||
|
|
||||||
// if player was loaded early
|
|
||||||
if (hashMap.containsKey(player))
|
|
||||||
{
|
|
||||||
return hashMap.get(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
// if world in what is player has giftapi
|
|
||||||
if (WorldNBTSynchronizer.hasAll(entityPlayer, worldPlayer))
|
|
||||||
{
|
|
||||||
NBTTagCompound compound = new BasicNBT(worldPlayer).readPlayerNBT(entityPlayer);
|
|
||||||
NBTTagList list = compound.getList("giftapi");
|
|
||||||
|
|
||||||
PageManager new_pages = new PageManager(list, this.max_stack);
|
|
||||||
hashMap.put(player, new_pages);
|
|
||||||
|
|
||||||
return new_pages;
|
|
||||||
}
|
|
||||||
|
|
||||||
String world = WorldNBTSynchronizer.getWorldWhatHasPlayer(entityPlayer);
|
|
||||||
|
|
||||||
// if world in what is player hasn't giftapi, but in another has
|
|
||||||
if (world != null)
|
|
||||||
{
|
|
||||||
NBTTagCompound compound = new BasicNBT(world).readPlayerNBT(entityPlayer);
|
|
||||||
NBTTagList list = compound.getList("giftapi");
|
|
||||||
|
|
||||||
PageManager new_pages = new PageManager(list, this.max_stack);
|
|
||||||
hashMap.put(player, new_pages);
|
|
||||||
|
|
||||||
return new_pages;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if player doesn't have any saved apiconfig NBT
|
|
||||||
PageManager new_pages = new PageManager(this.max_stack);
|
|
||||||
hashMap.put(player, new_pages);
|
|
||||||
|
|
||||||
return new_pages;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void disable()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void playerQuit(String playerName)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<Player, PageManager> getWorlMap(String worldName)
|
|
||||||
{
|
|
||||||
switch(worldName)
|
|
||||||
{
|
|
||||||
case "world": { return this.world_map; }
|
|
||||||
case "world_nether": { return this.nether_map; }
|
|
||||||
case "world_the_end": { return this.end_map; }
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
package tokarotik.giftapi.NBT.cache;
|
|
||||||
|
|
||||||
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
|
||||||
import net.minecraft.server.v1_6_R3.NBTTagCompound;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.World;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public class WorldNBTSynchronizer
|
|
||||||
{
|
|
||||||
public static boolean isPlayerEmpty(EntityPlayer entityPlayer)
|
|
||||||
{
|
|
||||||
return getWorldWhatHasPlayer(entityPlayer) == null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static String getWorldWhatHasPlayer(EntityPlayer entityPlayer)
|
|
||||||
{
|
|
||||||
for (World world : Bukkit.getWorlds())
|
|
||||||
{
|
|
||||||
String worldName = world.getName();
|
|
||||||
|
|
||||||
if (hasAll(entityPlayer, worldName))
|
|
||||||
{
|
|
||||||
return worldName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean hasAll(EntityPlayer entityPlayer, String worldName)
|
|
||||||
{
|
|
||||||
boolean isHasPlayer = hasPlayer(entityPlayer.getName(), worldName);
|
|
||||||
if (!isHasPlayer) { return false; }
|
|
||||||
|
|
||||||
return hasGiftApiNBT(entityPlayer, worldName);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean hasPlayer(String playerName, String worldName)
|
|
||||||
{
|
|
||||||
World world = Bukkit.getWorld(worldName);
|
|
||||||
|
|
||||||
for (Player player : world.getPlayers())
|
|
||||||
{
|
|
||||||
if (player.getName().equals(playerName)) { return true; }
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean hasGiftApiNBT(EntityPlayer entityPlayer, String worldName)
|
|
||||||
{
|
|
||||||
BasicNBT basicNBT = new BasicNBT(worldName);
|
|
||||||
|
|
||||||
NBTTagCompound tag = basicNBT.readPlayerNBT(entityPlayer);
|
|
||||||
|
|
||||||
if (tag.hasKey("giftapi")) // if exists giftapi tag in player
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
tag.getList("giftapi");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
catch(Exception ignored){} // if giftapi is not type list, throw exception and for what I used construction try-catch.
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -18,9 +18,7 @@ public class PageManager
|
|||||||
|
|
||||||
public PageManager(int max_stack)
|
public PageManager(int max_stack)
|
||||||
{
|
{
|
||||||
|
|
||||||
this.max_stack = max_stack;
|
this.max_stack = max_stack;
|
||||||
this.tag = new NBTTagList("giftapi");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public NBTTagList getTag()
|
public NBTTagList getTag()
|
||||||
|
@ -6,6 +6,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import tokarotik.giftapi.APIManager;
|
import tokarotik.giftapi.APIManager;
|
||||||
|
import tokarotik.giftapi.NBT.NBTManager;
|
||||||
|
|
||||||
public class GiftCommand implements CommandExecutor {
|
public class GiftCommand implements CommandExecutor {
|
||||||
|
|
||||||
@ -24,6 +25,14 @@ public class GiftCommand implements CommandExecutor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new NBTManager("world").write((Player) sender);
|
||||||
|
|
||||||
|
//BasicNBT nbt = new BasicNBT("world");
|
||||||
|
|
||||||
|
//System.out.println(nbt.readPlayerNBT(nbt.getPlayer((Player) sender)).getCompound("giftapi"));
|
||||||
|
|
||||||
|
//((Player) sender).getInventory().addItem(new ItemLoad(nbt.readPlayerNBT(nbt.getPlayer((Player) sender)).getCompound("giftapi")).getItem());
|
||||||
|
|
||||||
this.apiManager.openInventory((Player) sender);
|
this.apiManager.openInventory((Player) sender);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user