Skip to content

Commit

Permalink
update API
Browse files Browse the repository at this point in the history
  • Loading branch information
StarchierOrb committed Aug 18, 2021
1 parent 213a06b commit b7565ef
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

public final class InventoryKeeper extends JavaPlugin {
public static boolean papiEnabled = false;
Expand Down Expand Up @@ -64,7 +63,7 @@ public void onEnable() {
CommandExec commandExec = new CommandExec(ph, this);
Bukkit.getPluginManager().registerEvents(new DeathHandler(this, dataManager, commandExec, ph), this);
Bukkit.getPluginManager().registerEvents(new EntityDamageListener(), this);
Bukkit.getPluginManager().registerEvents(new RespawnHandler(commandExec, ph, this), this);
Bukkit.getPluginManager().registerEvents(new RespawnHandler(commandExec, ph, this, dataManager), this);
Bukkit.getPluginManager().registerEvents(new PlayerDataInit(dataManager), this);
//Bukkit.getPluginManager().registerEvents(new InventoryClickHandler(this),this);
Bukkit.getPluginManager().registerEvents(new BlockPlaceListener(ih, ph), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class PlayerConsumeItemEvent extends Event {
private final ItemBase item;
private final PluginHandler pluginHandler;
private final DataManager dataManager;
private final int consumeType;

public PlayerConsumeItemEvent(Player player, ItemBase item, PluginHandler pluginHandler, DataManager dataManager) {
public PlayerConsumeItemEvent(Player player, ItemBase item, PluginHandler pluginHandler, DataManager dataManager, int consumeType) {
this.player = player;
this.item = item;
this.pluginHandler = pluginHandler;
this.dataManager = dataManager;
this.consumeType = consumeType;
}

public PluginHandler getPluginHandler() {
Expand All @@ -44,4 +46,8 @@ public Player getPlayer() {
public ItemBase getItem() {
return item;
}

public int getConsumeType() {
return consumeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

public class PlayerRespawnWithItemEvent extends Event {
public class RespawnCompleteEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final Player player;
private final ItemBase item;
private final PluginHandler pluginHandler;
private final DataManager dataManager;

public PlayerRespawnWithItemEvent(Player player, ItemBase item, PluginHandler pluginHandler, DataManager dataManager) {
public RespawnCompleteEvent(Player player, ItemBase item, PluginHandler pluginHandler, DataManager dataManager) {
this.player = player;
this.item = item;
this.pluginHandler = pluginHandler;
Expand Down
42 changes: 20 additions & 22 deletions src/main/java/me/starchier/inventorykeeper/events/DeathHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import me.starchier.inventorykeeper.items.ItemBase;
import me.starchier.inventorykeeper.storage.PlayerInventoryStorage;
import me.starchier.inventorykeeper.storage.PlayerStorage;
import me.starchier.inventorykeeper.util.DataManager;
import me.starchier.inventorykeeper.util.Debugger;
import me.starchier.inventorykeeper.util.ExpHandler;
import me.starchier.inventorykeeper.util.PluginHandler;
import me.starchier.inventorykeeper.util.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameRule;
Expand Down Expand Up @@ -46,11 +43,6 @@ public DeathHandler(InventoryKeeper plugin, DataManager dataManager, CommandExec

@EventHandler
public void onPlayerDeath(PlayerDeathEvent evt) {
//Consume Type
int CONSUME_NONE = -1;
int CONSUME_PERMISSION = 0;
int CONSUME_PHYSICAL = 1;
int CONSUME_VIRTUAL = 2;

//DEBUG OPTION
if (pluginHandler.getBooleanConfigValue("show-death-cause-on-death", true)) {
Expand All @@ -76,7 +68,7 @@ public void onPlayerDeath(PlayerDeathEvent evt) {
//Shared list
List<String> passedItems = new ArrayList<>();
String playerWorld = evt.getEntity().getWorld().getName();
int consumeType = CONSUME_NONE;
int consumeType = ConsumeType.CONSUME_NONE;
ItemBase[] consumeItems = new ItemBase[3];

//Permission check
Expand All @@ -96,8 +88,8 @@ public void onPlayerDeath(PlayerDeathEvent evt) {
if (!passedItems.isEmpty()) {
for (ItemBase itemBase : pluginHandler.currentItems) {
if (passedItems.contains(itemBase.getName())) {
consumeItems[CONSUME_PERMISSION] = itemBase;
consumeType = CONSUME_PERMISSION;
consumeItems[ConsumeType.CONSUME_PERMISSION] = itemBase;
consumeType = ConsumeType.CONSUME_PERMISSION;
break;
}
}
Expand Down Expand Up @@ -131,8 +123,8 @@ public void onPlayerDeath(PlayerDeathEvent evt) {
for (ItemBase itemBase : pluginHandler.currentItems) {
if (passedPhysicalItems.containsKey(itemBase.getName())) {
physicalSlot = passedPhysicalItems.get(itemBase.getName());
consumeItems[CONSUME_PHYSICAL] = itemBase;
consumeType = CONSUME_PHYSICAL;
consumeItems[ConsumeType.CONSUME_PHYSICAL] = itemBase;
consumeType = ConsumeType.CONSUME_PHYSICAL;
break;
}
}
Expand All @@ -157,19 +149,22 @@ public void onPlayerDeath(PlayerDeathEvent evt) {
if (!passedItems.isEmpty()) {
for (ItemBase itemBase : pluginHandler.currentItems) {
if (passedItems.contains(itemBase.getName())) {
consumeItems[CONSUME_VIRTUAL] = itemBase;
consumeType = CONSUME_VIRTUAL;
consumeItems[ConsumeType.CONSUME_VIRTUAL] = itemBase;
consumeType = ConsumeType.CONSUME_VIRTUAL;
break;
}
}
}
if (consumeType != CONSUME_NONE) {
ItemBase highestItem = consumeItems[consumeType];
if (consumeType != ConsumeType.CONSUME_NONE) {
ItemBase highestItem = null;
for (int i = 0; i < consumeItems.length; i++) {
if (consumeItems[i] == null) {
continue;
}
if (consumeItems[i].getPriority() > highestItem.getPriority()) {
if (highestItem == null) {
highestItem = consumeItems[i];
consumeType = i;
} else if (consumeItems[i].getPriority() > highestItem.getPriority()) {
highestItem = consumeItems[i];
consumeType = i;
}
Expand Down Expand Up @@ -203,7 +198,7 @@ public void onPlayerDeath(PlayerDeathEvent evt) {
PlayerStorage.isKeep.put(evt.getEntity(), true);
boolean clearVanish = pluginHandler.getBooleanConfigValue("clear-vanishing-curse-items", true);
boolean dropBinding = pluginHandler.getBooleanConfigValue("drop-binding-curse-items", true);
if (consumeType == CONSUME_PHYSICAL) {
if (consumeType == ConsumeType.CONSUME_PHYSICAL) {
ItemStack targetItem = evt.getEntity().getInventory().getItem(physicalSlot);
int amount = targetItem.getAmount() - 1;
if (amount <= 0) {
Expand All @@ -213,7 +208,7 @@ public void onPlayerDeath(PlayerDeathEvent evt) {
}
evt.getEntity().getInventory().setItem(physicalSlot, targetItem);
Debugger.logDebugMessage(evt.getEntity().getName() + " died, target slot:" + physicalSlot);
} else if (consumeType == CONSUME_VIRTUAL) {
} else if (consumeType == ConsumeType.CONSUME_VIRTUAL) {
dataManager.setConsumed(evt.getEntity(), consumeItems[consumeType].getName());
}
int i = 0;
Expand All @@ -227,9 +222,11 @@ public void onPlayerDeath(PlayerDeathEvent evt) {
if (isModern) {
try {
if (clearVanish && item.containsEnchantment(Enchantment.VANISHING_CURSE)) {
Debugger.logDebugMessage("vanishing curse item slot: " + i);
evt.getEntity().getInventory().setItem(i, null);
}
if (dropBinding && item.containsEnchantment(Enchantment.BINDING_CURSE)) {
Debugger.logDebugMessage("binding curse item slot: " + i);
evt.getEntity().getWorld().dropItem(evt.getEntity().getLocation(), item);
evt.getEntity().getInventory().setItem(i, null);
}
Expand All @@ -255,8 +252,9 @@ public void onPlayerDeath(PlayerDeathEvent evt) {
}
i++;
}
Debugger.logDebugMessage("inventory size: " + i + " / " + evt.getEntity().getInventory().getSize());
Bukkit.getServer().getPluginManager().callEvent(new PlayerConsumeItemEvent(evt.getEntity(), consumeItems[consumeType],
pluginHandler, dataManager));
pluginHandler, dataManager, consumeType));
if (pluginHandler.compatInventory) {
PlayerStorage.saveInventory(evt.getEntity(), new PlayerInventoryStorage(evt.getEntity()));
evt.getEntity().getInventory().clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.starchier.inventorykeeper.events;

import me.starchier.inventorykeeper.InventoryKeeper;
import me.starchier.inventorykeeper.api.events.PlayerRespawnWithItemEvent;
import me.starchier.inventorykeeper.api.events.RespawnCompleteEvent;
import me.starchier.inventorykeeper.command.CommandExec;
import me.starchier.inventorykeeper.items.FoodLevel;
import me.starchier.inventorykeeper.items.ItemBase;
Expand Down Expand Up @@ -98,7 +98,7 @@ public void run() {
private void callEvent(Player player, ItemBase item) {
new BukkitRunnable() {
public void run() {
Bukkit.getServer().getPluginManager().callEvent(new PlayerRespawnWithItemEvent(player,
Bukkit.getServer().getPluginManager().callEvent(new RespawnCompleteEvent(player,
item, pluginHandler, dataManager));
}
}.runTaskLater(plugin, 12);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.starchier.inventorykeeper.util;

public class ConsumeType {
public static final int CONSUME_NONE = -1;
public static final int CONSUME_PERMISSION = 0;
public static final int CONSUME_PHYSICAL = 1;
public static final int CONSUME_VIRTUAL = 2;
}
1 change: 1 addition & 0 deletions src/main/resources/locales/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Admin permission: inventorykeeper.admin The plugin only works when the world ga
# Compatibility Mode - if the plugin does not work properly, try to enable it.\n\
# inventory - set it to true if the item is consumed but the inventory still drops.\n\
# exp - set it to true if the experience level can not be set properly on respawn.\n\
# Recommended to set gamerule doImmediateRespawn to true if compatibility-mode enabled to prevent player lose inventory when server restart or crash. For older versions, try to use an automatic respawn plugin.\n\
compatibility-mode:\n\
inventory: false\n\
exp: false\n\
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/locales/messages_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ InventoryKeeper 配置文件\n\
# 如果默认情况下插件正常运行请不要开启。\n\
# inventory - 消耗护符后背包物品仍然掉落时尝试开启该选项.\n\
# exp - 如果玩家复活后经验值数量异常,比如等级直接被重置成0,可以尝试开启该选项.\n\
# 如果启用了兼容模式,建议将游戏规则doImmediateRespawn设置为true(自动复活),低版本可尝试使用自动复活插件。\n\
# 因为兼容模式下背包内容由插件存储并控制,如果服务器崩溃或重启时玩家处于死亡状态则有可能导致玩家背包内容丢失。\n\
compatibility-mode:\n\
inventory: false\n\
exp: false\n\
Expand Down

0 comments on commit b7565ef

Please sign in to comment.