Skip to content

Commit

Permalink
简化代码;修复无法补充食物的Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
0XPYEX0 committed Dec 17, 2022
1 parent 8f1d214 commit 5214a20
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
import org.bukkit.block.Block;
import org.bukkit.block.Container;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.ThrowableProjectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemBreakEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.EquipmentSlot;
Expand Down Expand Up @@ -64,6 +67,12 @@ public class HandleEvent implements Listener {
}
}

@EventHandler(ignoreCancelled = true)
public void onEat(PlayerItemConsumeEvent event) {
SortUtil.replaceTool(event.getPlayer(), new ItemStack(event.getItem()));
//
}

@EventHandler(ignoreCancelled = true)
public void onPressFWithoutInv(PlayerSwapHandItemsEvent event) {
if (event.getPlayer().isSneaking()) { //Shift+F
Expand Down Expand Up @@ -126,25 +135,7 @@ public void onShoot(ProjectileLaunchEvent event) {
if (event.getEntity() instanceof ThrowableProjectile && event.getEntity().getType() != EntityType.TRIDENT) { //扔的还是投掷物
ItemStack before = new ItemStack(((ThrowableProjectile) event.getEntity()).getItem());
Player p = (Player) event.getEntity().getShooter();
EquipmentSlot slot = ItemUtil.equals(before, p.getInventory().getItemInMainHand()) ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND;
JsonObject o = ConfigUtil.getConfig(SortItems.getInstance(), "players/" + p.getUniqueId());
if (o.get("ReplaceBrokenTool").getAsBoolean()) { //如果玩家开启了替换手中道具
Bukkit.getScheduler().runTaskLater(SortItems.getInstance(), () -> {
if (p.getInventory().getItem(slot).getType() == Material.AIR) {
for (ItemStack content : p.getInventory().getContents()) { //不遍历盔甲
if (content == null) continue;

if (ItemUtil.equals(content, before)) {
ItemStack copied = new ItemStack(content);
p.getInventory().setItem(slot, copied);
content.setAmount(0);
MsgUtil.sendActionBar(p, "&a您的道具已用尽,从背包补全. &e该功能在 &f/SortItems &e中调整");
return;
}
}
}
}, 1L);
}
SortUtil.replaceTool(p, before);
}
}
}
Expand All @@ -165,25 +156,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
}

ItemStack before = new ItemStack(event.getItem());

JsonObject o = ConfigUtil.getConfig(SortItems.getInstance(), "players/" + event.getPlayer().getUniqueId());
if (o.get("ReplaceBrokenTool").getAsBoolean()) { //如果玩家开启了替换手中道具
Bukkit.getScheduler().runTaskLater(SortItems.getInstance(), () -> {
if (event.getPlayer().getInventory().getItem(event.getHand()).getType() == Material.AIR) {
for (ItemStack content : event.getPlayer().getInventory().getContents()) { //不遍历盔甲
if (content == null) continue;

if (ItemUtil.equals(content, before)) {
ItemStack copied = new ItemStack(content);
event.getPlayer().getInventory().setItem(event.getHand(), copied);
content.setAmount(0);
MsgUtil.sendActionBar(event.getPlayer(), "&a您的道具已用尽,从背包补全. &e该功能在 &f/SortItems &e中调整");
return;
}
}
}
}, 1L);
}
SortUtil.replaceTool(event.getPlayer(), before);
}

@EventHandler
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/me/xpyex/plugin/sortitems/bukkit/util/SortUtil.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package me.xpyex.plugin.sortitems.bukkit.util;

import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.TreeMap;
import me.xpyex.plugin.sortitems.bukkit.SortItems;
import me.xpyex.plugin.sortitems.bukkit.enums.ItemType;
import me.xpyex.plugin.xplib.bukkit.api.Pair;
import me.xpyex.plugin.xplib.bukkit.util.config.ConfigUtil;
import me.xpyex.plugin.xplib.bukkit.util.inventory.ItemUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.MsgUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.StrUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
Expand Down Expand Up @@ -171,4 +178,26 @@ else if (pair.getKey().getType().isBlock()) //其余方块
}
}
}

public static void replaceTool(Player p, ItemStack before) {
EquipmentSlot slot = ItemUtil.equals(before, p.getInventory().getItemInMainHand()) ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND;
JsonObject o = ConfigUtil.getConfig(SortItems.getInstance(), "players/" + p.getUniqueId());
if (o.get("ReplaceBrokenTool").getAsBoolean()) { //如果玩家开启了替换手中道具
Bukkit.getScheduler().runTaskLater(SortItems.getInstance(), () -> {
if (p.getInventory().getItem(slot).getType() == Material.AIR) {
for (ItemStack content : p.getInventory().getContents()) { //不遍历盔甲
if (content == null) continue;

if (ItemUtil.equals(content, before)) {
ItemStack copied = new ItemStack(content);
p.getInventory().setItem(slot, copied);
content.setAmount(0);
MsgUtil.sendActionBar(p, "&a您的道具已用尽,从背包补全. &e该功能在 &f/SortItems &e中调整");
return;
}
}
}
}, 1L);
}
}
}

0 comments on commit 5214a20

Please sign in to comment.