Skip to content

Commit

Permalink
perf: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
0XPYEX0 committed Jun 24, 2024
1 parent 4a6907e commit 7ef7a13
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Collection;
import me.xpyex.plugin.invactions.bukkit.InvActions;
import me.xpyex.plugin.invactions.bukkit.config.InvActionsServerConfig;
import me.xpyex.plugin.invactions.bukkit.util.SettingsUtil;
import me.xpyex.plugin.xplib.bukkit.util.inventory.ItemUtil;
import me.xpyex.plugin.xplib.bukkit.util.language.LangUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.MsgUtil;
Expand All @@ -22,7 +21,7 @@
public class AutoFarmer extends RootModule {
@EventHandler(ignoreCancelled = true)
public void onRightClick(PlayerInteractEvent event) {
if (InvActionsServerConfig.getConfig().AutoFarmer && SettingsUtil.getConfig(event.getPlayer()).AutoFarmer) { //如果玩家开启自动收割
if (serverEnabled() && playerEnabled(event.getPlayer())) { //如果玩家开启自动收割
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (ItemUtil.typeIsOr(event.getClickedBlock(), Material.MELON, Material.PUMPKIN)) {
if (InvActionsServerConfig.getConfig().AutoFarmer_AllowPumpkinAndMelon) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package me.xpyex.plugin.invactions.bukkit.module;

import java.util.HashMap;
import me.xpyex.plugin.invactions.bukkit.config.InvActionsServerConfig;
import me.xpyex.plugin.invactions.bukkit.enums.ItemType;
import me.xpyex.plugin.invactions.bukkit.enums.ToolType;
import me.xpyex.plugin.invactions.bukkit.util.InvUtil;
import me.xpyex.plugin.invactions.bukkit.util.SettingsUtil;
import me.xpyex.plugin.xplib.bukkit.api.Pair;
import me.xpyex.plugin.xplib.bukkit.util.strings.MsgUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.StrUtil;
Expand All @@ -32,7 +30,7 @@ public class AutoTool extends RootModule {

@EventHandler(ignoreCancelled = true)
public void onInteract(PlayerInteractEvent event) {
if (!InvActionsServerConfig.getConfig().AutoTool || !SettingsUtil.getConfig(event.getPlayer()).AutoTool)
if (!serverEnabled() || !playerEnabled(event.getPlayer()))
return;

if (event.getPlayer().getGameMode() == GameMode.CREATIVE || event.getPlayer().getGameMode() == GameMode.SPECTATOR)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package me.xpyex.plugin.invactions.bukkit.module;

import me.xpyex.plugin.invactions.bukkit.InvActions;
import me.xpyex.plugin.invactions.bukkit.config.InvActionsServerConfig;
import me.xpyex.plugin.invactions.bukkit.util.InvUtil;
import me.xpyex.plugin.invactions.bukkit.util.SettingsUtil;
import me.xpyex.plugin.xplib.bukkit.util.inventory.ItemUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand All @@ -21,7 +19,7 @@
public class BetterInfinity extends RootModule {
@EventHandler
public void onRightClick(PlayerInteractEvent event) {
if (InvActionsServerConfig.getConfig().BetterInfinity && SettingsUtil.getConfig(event.getPlayer()).BetterInfinity) {
if (serverEnabled() && playerEnabled(event.getPlayer())) {
if (event.getAction().toString().startsWith("RIGHT_") && event.getItem() != null && event.getHand() != null) {
if (event.getItem().getType() == Material.BOW) {
PlayerInventory playerInv = event.getPlayer().getInventory();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package me.xpyex.plugin.invactions.bukkit.module;

import me.xpyex.plugin.invactions.bukkit.InvActions;
import me.xpyex.plugin.invactions.bukkit.config.InvActionsServerConfig;
import me.xpyex.plugin.invactions.bukkit.util.SettingsUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.MsgUtil;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -19,13 +17,13 @@ public class BetterLoyalty extends RootModule {

@EventHandler
public void onProjectileLaunch(ProjectileLaunchEvent event) {
if (!InvActionsServerConfig.getConfig().BetterLoyalty) { //服务器未启用
if (!serverEnabled()) { //服务器未启用
return;
}
if (event.getEntity() instanceof Trident) {
if (event.getEntity().getShooter() instanceof Player) {
Player shooter = (Player) event.getEntity().getShooter();
if (!SettingsUtil.getConfig(shooter).BetterLoyalty) { //玩家未启用
if (!playerEnabled(shooter)) { //玩家未启用
return;
}
if (((Trident) event.getEntity()).getItem().getEnchantments().containsKey(Enchantment.LOYALTY)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package me.xpyex.plugin.invactions.bukkit.module;

import me.xpyex.plugin.invactions.bukkit.config.InvActionsServerConfig;
import me.xpyex.plugin.invactions.bukkit.util.InvUtil;
import me.xpyex.plugin.invactions.bukkit.util.SettingsUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.MsgUtil;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -13,10 +11,10 @@
public class CraftDrop extends RootModule {
@EventHandler(ignoreCancelled = true)
public void onCraft(CraftItemEvent event) {
if (!(event.getWhoClicked() instanceof Player)) return;
if (!serverEnabled()) return;

if (!InvActionsServerConfig.getConfig().CraftDrop || !SettingsUtil.getConfig((Player) event.getWhoClicked()).CraftDrop)
return;
if (!(event.getWhoClicked() instanceof Player)) return;
if (!playerEnabled(((Player) event.getWhoClicked()))) return;

if (event.getClick() != ClickType.CONTROL_DROP) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import java.util.UUID;
import java.util.WeakHashMap;
import me.xpyex.plugin.invactions.bukkit.InvActions;
import me.xpyex.plugin.invactions.bukkit.config.InvActionsServerConfig;
import me.xpyex.plugin.invactions.bukkit.enums.ItemType;
import me.xpyex.plugin.invactions.bukkit.util.SettingsUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.MsgUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.StrUtil;
import org.bukkit.Bukkit;
Expand All @@ -30,9 +28,9 @@ public class DynamicLight extends RootModule {

public void registerTask() {
Bukkit.getScheduler().runTaskTimerAsynchronously(InvActions.getInstance(), () -> {
if (InvActionsServerConfig.getConfig().DynamicLight) { //服务端启用动态光源
if (serverEnabled()) { //服务端启用动态光源
for (Player player : Bukkit.getOnlinePlayers()) { //遍历玩家
if (SettingsUtil.getConfig(player).DynamicLight) { //玩家启用动态光源
if (playerEnabled(player)) { //玩家启用动态光源
Material toolType = player.getInventory().getItemInMainHand().getType();
Material offhandType = player.getInventory().getItemInOffHand().getType();

Expand Down Expand Up @@ -67,7 +65,7 @@ public void registerTask() {

@EventHandler
public void onShoot(ProjectileLaunchEvent event) {
if (!InvActionsServerConfig.getConfig().DynamicLight) return;
if (!serverEnabled()) return;

Projectile projectile = event.getEntity();
new BukkitRunnable() {
Expand All @@ -77,7 +75,7 @@ public void onShoot(ProjectileLaunchEvent event) {
public void run() {
if (projectile.isValid() && projectile.getFireTicks() > 0) {
Bukkit.getOnlinePlayers().forEach(player -> {
if (SettingsUtil.getConfig(player).DynamicLight) {
if (playerEnabled(player)) {
if (player.getWorld().equals(lastLoc.getWorld())) {
player.sendBlockChange(lastLoc, lastLoc.getBlock().getBlockData());
lastLoc = projectile.getLocation().clone();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.xpyex.plugin.invactions.bukkit.module;

import me.xpyex.plugin.invactions.bukkit.config.InvActionsServerConfig;
import me.xpyex.plugin.invactions.bukkit.util.SettingsUtil;
import me.xpyex.plugin.xplib.bukkit.util.inventory.ItemUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.MsgUtil;
import org.bukkit.event.EventHandler;
Expand All @@ -11,23 +9,23 @@
public class QuickDrop extends RootModule {
@EventHandler(ignoreCancelled = true)
public void onDrop(PlayerDropItemEvent event) {
if (!InvActionsServerConfig.getConfig().QuickDrop || !SettingsUtil.getConfig(event.getPlayer()).QuickDrop) {
if (event.getPlayer().isSneaking()) {
ItemStack drop = event.getItemDrop().getItemStack();
if (event.getPlayer().getInventory().contains(drop.getType())) {
for (ItemStack content : event.getPlayer().getInventory().getStorageContents()) {
if (content == null) continue;
if (!serverEnabled()) return; //服务端未启用
if (!playerEnabled(event.getPlayer())) return; //玩家未启用
if (event.getPlayer().isSneaking()) {
ItemStack drop = event.getItemDrop().getItemStack();
if (event.getPlayer().getInventory().contains(drop.getType())) {
for (ItemStack content : event.getPlayer().getInventory().getStorageContents()) {
if (content == null) continue;

if (ItemUtil.equals(content, drop)) {
ItemStack copied = new ItemStack(content);
event.getPlayer().getInventory().setItemInMainHand(copied);
content.setAmount(0);
event.getPlayer().dropItem(true);
}
if (ItemUtil.equals(content, drop)) {
ItemStack copied = new ItemStack(content);
event.getPlayer().getInventory().setItemInMainHand(copied);
content.setAmount(0);
event.getPlayer().dropItem(true);
}
MsgUtil.sendActionBar(event.getPlayer(), getMessageWithSuffix("drop"));
event.getPlayer().updateInventory();
}
MsgUtil.sendActionBar(event.getPlayer(), getMessageWithSuffix("drop"));
event.getPlayer().updateInventory();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
public class QuickMove extends RootModule {
@EventHandler
public void onClick(InventoryClickEvent event) {
if (!serverEnabled()) return;
if (!(event.getWhoClicked() instanceof Player)) {
return;
}
if (!playerEnabled(((Player) event.getWhoClicked()))) return;
if (event.getWhoClicked().hasMetadata("InvActions_CallingClick")) { //正在广播事件给插件处理,自己无需处理
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package me.xpyex.plugin.invactions.bukkit.module;

import me.xpyex.plugin.invactions.bukkit.InvActions;
import me.xpyex.plugin.invactions.bukkit.config.InvActionsServerConfig;
import me.xpyex.plugin.invactions.bukkit.util.SettingsUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.BlockState;
import org.bukkit.block.ShulkerBox;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
Expand All @@ -26,8 +23,8 @@ public class QuickShulkerBox extends RootModule {
@EventHandler
public void onInvClick(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player)) return;
if (InvActionsServerConfig.getConfig().QuickShulkerBox) {
if (SettingsUtil.getConfig((Player) event.getWhoClicked()).QuickShulkerBox) {
if (serverEnabled()) {
if (playerEnabled((Player) event.getWhoClicked())) {
if (event.isShiftClick() && event.isRightClick()) {
if (event.getCursor() != null && event.getCursor().getType() != Material.AIR)
return;
Expand All @@ -49,13 +46,13 @@ public void onInvClick(InventoryClickEvent event) {

@EventHandler
public void onRightClick(PlayerInteractEvent event) {
if (!InvActionsServerConfig.getConfig().QuickShulkerBox) {
if (!serverEnabled()) {
return;
}
if (!SettingsUtil.getConfig(event.getPlayer()).QuickShulkerBox) {
if (!playerEnabled(event.getPlayer())) {
return;
}
if (event.getPlayer().isSneaking() && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
if (event.getPlayer().isSneaking() && event.getAction().toString().startsWith("RIGHT_")) {
if (event.getItem() != null && event.getItem().hasItemMeta()) {
ItemMeta meta = event.getItem().getItemMeta();
if (meta instanceof BlockStateMeta) {
Expand All @@ -75,7 +72,7 @@ public void onRightClick(PlayerInteractEvent event) {
public void onDrop(PlayerDropItemEvent event) {
if (event.getPlayer().hasMetadata(METADATA_KEY)) {
event.setCancelled(true);
event.getPlayer().updateInventory();
Bukkit.getScheduler().runTaskLater(InvActions.getInstance(), () -> event.getPlayer().updateInventory(), 2L);
}
}

Expand Down Expand Up @@ -108,7 +105,7 @@ public void onInvClose(InventoryCloseEvent event) {
((Player) event.getPlayer()).playSound(event.getPlayer().getLocation(), Sound.BLOCK_SHULKER_BOX_CLOSE, 1f, 1f);
Bukkit.getScheduler().runTaskLater(InvActions.getInstance(), () -> {
((Player) event.getPlayer()).updateInventory();
}, 1L);
}, 2L);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package me.xpyex.plugin.invactions.bukkit.module;

import me.xpyex.plugin.invactions.bukkit.InvActions;
import me.xpyex.plugin.invactions.bukkit.config.InvActionsServerConfig;
import me.xpyex.plugin.invactions.bukkit.util.InvUtil;
import me.xpyex.plugin.invactions.bukkit.util.SettingsUtil;
import me.xpyex.plugin.xplib.bukkit.util.strings.MsgUtil;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
Expand All @@ -27,7 +25,7 @@ public void onItemBreak(PlayerItemBreakEvent event) {
case OFF_HAND:
return;
default:
if (!InvActionsServerConfig.getConfig().ReplaceBrokenArmor || !SettingsUtil.getConfig(event.getPlayer()).ReplaceBrokenArmor) { //如果玩家未开启替换盔甲
if (!serverEnabled() || !playerEnabled(event.getPlayer())) { //如果玩家未开启替换盔甲
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ public Button getMenuButton(Menu menu) {
private String[] getMenuLore(int state) {
ArrayList<String> list = new ArrayList<>(getNationalMessages("menu.lore")); //不确定List具体类型,copy一份
list.add("");
list.add(LangUtil.getMessage(InvActions.getInstance(),
"State.current") + " " + LangUtil.getMessage(InvActions.getInstance(), "State." + (state == 1 ? "player_enabled" : (state == 0 ? "player_disabled" : "server_disabled")))
list.add(LangUtil.getMessage(InvActions.getInstance(), "State.current")
+ ": "
+ LangUtil.getMessage(InvActions.getInstance(), "State." + (state == 1 ? "player_enabled" : (state == 0 ? "player_disabled" : "server_disabled")))
);
return list.toArray(new String[0]);
}
Expand Down

0 comments on commit 7ef7a13

Please sign in to comment.