some changes
This commit is contained in:
parent
f347c01426
commit
1539130afe
@ -10,6 +10,7 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.jetbrains:annotations:24.0.0'
|
||||
testImplementation platform('org.junit:junit-bom:5.9.1')
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
implementation(
|
||||
|
@ -1,36 +1,49 @@
|
||||
package tokarotik.giftapi;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import tokarotik.giftapi.NBT.cache.CacheManager;
|
||||
import tokarotik.giftapi.dev.GiftCommand;
|
||||
|
||||
public class Main extends JavaPlugin implements Listener
|
||||
{
|
||||
private CacheManager cacheManager;
|
||||
private APIManager apiManager;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
public void onEnable()
|
||||
{
|
||||
saveDefaultConfig();
|
||||
|
||||
getLogger().info("GiftAPI enabled!");
|
||||
|
||||
saveDefaultConfig();
|
||||
cacheManager = new CacheManager(54);
|
||||
apiManager = new APIManager(this, getConfig());
|
||||
|
||||
if (getConfig().getBoolean(ConfigPaths.DEVTEST, false))
|
||||
{
|
||||
this.getCommand("gift").setExecutor(new GiftCommand(apiManager));
|
||||
}
|
||||
this.getCommand("gift").setExecutor(new GiftCommand(apiManager)); // DELETE FOR RELEASE
|
||||
|
||||
getServer().getPluginManager().registerEvents(this, this); // bullshit
|
||||
|
||||
getLogger().info("GiftAPI enabled!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
public void onDisable()
|
||||
{
|
||||
cacheManager.disable();
|
||||
|
||||
getLogger().info("GiftAPI disabled!");
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event)
|
||||
{
|
||||
cacheManager.playerQuit(event.getPlayer().getName());
|
||||
}
|
||||
|
||||
// if failed to add, return false
|
||||
// if successfully added, return true
|
||||
public boolean add(Player player, ItemStack item)
|
||||
|
@ -3,6 +3,7 @@ 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;
|
||||
import tokarotik.giftapi.NBT.cache.BasicNBT;
|
||||
|
||||
public class NBTManager
|
||||
{
|
||||
|
@ -1,40 +0,0 @@
|
||||
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,4 +1,4 @@
|
||||
package tokarotik.giftapi.NBT;
|
||||
package tokarotik.giftapi.NBT.cache;
|
||||
|
||||
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
||||
import net.minecraft.server.v1_6_R3.NBTCompressedStreamTools;
|
||||
@ -58,7 +58,7 @@ public class BasicNBT {
|
||||
}
|
||||
}
|
||||
|
||||
public EntityPlayer getPlayer(Player player)
|
||||
public static EntityPlayer getPlayer(Player player)
|
||||
{
|
||||
return ((CraftPlayer) player).getHandle();
|
||||
}
|
92
src/main/java/tokarotik/giftapi/NBT/cache/CacheManager.java
vendored
Normal file
92
src/main/java/tokarotik/giftapi/NBT/cache/CacheManager.java
vendored
Normal file
@ -0,0 +1,92 @@
|
||||
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;
|
||||
}
|
||||
}
|
83
src/main/java/tokarotik/giftapi/NBT/cache/WorldNBTSynchronizer.java
vendored
Normal file
83
src/main/java/tokarotik/giftapi/NBT/cache/WorldNBTSynchronizer.java
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
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;
|
||||
import tokarotik.giftapi.NBT.cache.BasicNBT;
|
||||
|
||||
public class WorldNBTSynchronizer
|
||||
{
|
||||
public static void getWorld(EntityPlayer entityPlayer, String worldName)
|
||||
{
|
||||
if (hasAll(entityPlayer, worldName)) { return; }
|
||||
|
||||
|
||||
}
|
||||
|
||||
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; }
|
||||
|
||||
boolean isHasNBT = hasGiftApiNBT(entityPlayer, worldName);
|
||||
return isHasNBT;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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,7 +18,9 @@ public class PageManager
|
||||
|
||||
public PageManager(int max_stack)
|
||||
{
|
||||
|
||||
this.max_stack = max_stack;
|
||||
this.tag = new NBTTagList("giftapi");
|
||||
}
|
||||
|
||||
public NBTTagList getTag()
|
||||
|
Loading…
x
Reference in New Issue
Block a user