Skip to content

Commit

Permalink
修复Shift+F不需要低头就整理的Bug;新增AutoTool
Browse files Browse the repository at this point in the history
  • Loading branch information
0XPYEX0 committed Apr 29, 2023
1 parent bacc53b commit bfcbe80
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 68 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = 'me.xpyex.plugin.invactions.bukkit'
version = '1.1.6'
version = '1.1.7'

repositories {
mavenCentral()
Expand All @@ -19,7 +19,7 @@ repositories {

dependencies {
compileOnly fileTree(dir:'libs' ,include:['*.jar'])
compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
compileOnly 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT'
compileOnly 'org.jetbrains:annotations:23.1.0'
}

Expand Down
145 changes: 83 additions & 62 deletions src/main/java/me/xpyex/plugin/invactions/bukkit/InvActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.UUID;
import me.xpyex.plugin.invactions.bukkit.command.HandleCmd;
import me.xpyex.plugin.invactions.bukkit.listener.AutoFarmer;
import me.xpyex.plugin.invactions.bukkit.listener.AutoTool;
import me.xpyex.plugin.invactions.bukkit.listener.CraftDrop;
import me.xpyex.plugin.invactions.bukkit.listener.HandleEvent;
import me.xpyex.plugin.invactions.bukkit.listener.InventoryF;
Expand All @@ -22,86 +23,30 @@
import me.xpyex.plugin.xplib.bukkit.util.version.VersionUtil;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.plugin.java.JavaPlugin;

public final class InvActions extends JavaPlugin {
private static final String XPLIB_VER = "1.0.8";
public static final String[] LIGHTS = {"LANTERN", "TORCH", "GLOW", "SHROOMLIGHT", "FrogLight", "END_ROD", "CampFire", "LAVA"};
public static final String[] LIGHTS = {"LANTERN", "TORCH", "GLOW", "ShroomLight", "FrogLight", "END_ROD", "CampFire", "LAVA"};
private static InvActions INSTANCE;
private static final HashMap<UUID, Location> PLAYER_DYNAMIC_LIGHT = new HashMap<>();

@Override
public void onEnable() {
INSTANCE = this;
if (!getServer().getPluginManager().isPluginEnabled("XPLib")) {
getLogger().severe("本插件需要XPLib作为前置...");
getLogger().severe("请在下载后,再加载本插件");
getLogger().severe("GitHub: https://github.com/0XPYEX0/XPLib/releases");
getLogger().severe("Gitee(国内): https://gitee.com/XPYEX/XPLib/releases");
getServer().getPluginManager().disablePlugin(getInstance());
return;
}

if (VersionUtil.getMainVersion() < 9) {
getLogger().severe("本插件需要Minecraft至少为1.9才可运行");
getLogger().severe("很遗憾,您的服务器不满足此条件...");
getServer().getPluginManager().disablePlugin(getInstance());
return;
}

if (!VersionUtil.requireXPLib(new Version(XPLIB_VER))) {
getLogger().severe("请更新您服务器内的XPLib!");
getLogger().severe("当前XPLib无法支持本插件");
getLogger().severe("需要: " + XPLIB_VER + " , 当前: " + VersionUtil.getXPLibVersion().getVersion());
getLogger().severe("GitHub: https://github.com/0XPYEX0/XPLib/releases");
getLogger().severe("Gitee(国内): https://gitee.com/XPYEX/XPLib/releases");
if (!initCheck()) {
getServer().getPluginManager().disablePlugin(getInstance());
return;
}

{
ConfigUtil.saveConfig(getInstance(), "config", GsonUtil.parseStr(SettingsUtil.DEFAULT_SERVER_SETTINGS), false);
JsonObject o = GsonUtil.copy(SettingsUtil.DEFAULT_SERVER_SETTINGS);
JsonObject config = ConfigUtil.getConfig(getInstance());
for (String key : GsonUtil.getKeysOfJsonObject(config)) {
o.add(key, config.get(key));
}
ConfigUtil.saveConfig(getInstance(), "config", GsonUtil.parseStr(o), true);
} //更新服务端config.json

{
getServer().getScheduler().runTaskAsynchronously(getInstance(), () -> {
getServer().getOnlinePlayers().forEach((player -> {
ConfigUtil.saveConfig(InvActions.getInstance(), "players/" + player.getUniqueId(), GsonUtil.parseStr(SettingsUtil.DEFAULT_SETTINGS), false);
//如果文件还不存在,则新建一份
JsonObject before = ConfigUtil.getConfig(InvActions.getInstance(), "players/" + player.getUniqueId());
JsonObject out = GsonUtil.copy(SettingsUtil.DEFAULT_SETTINGS);
for (String setting : GsonUtil.getKeysOfJsonObject(SettingsUtil.DEFAULT_SETTINGS)) {
if (before.has(setting)) {
out.add(setting, before.get(setting));
}
} //更新设定,应对热拔插
ConfigUtil.saveConfig(InvActions.getInstance(), "players/" + player.getUniqueId(), GsonUtil.parseStr(out), true);
}));
});
} //更新玩家们的设定
updateServerConfig();
getServer().getScheduler().runTaskAsynchronously(getInstance(), this::updatePlayersConfig);

getServer().getPluginManager().registerEvents(new AutoFarmer(), getInstance());
getServer().getPluginManager().registerEvents(new CraftDrop(), getInstance());
getServer().getPluginManager().registerEvents(new HandleEvent(), getInstance());
getServer().getPluginManager().registerEvents(new QuickDrop(), getInstance());
getServer().getPluginManager().registerEvents(new QuickMove(), getInstance());
getServer().getPluginManager().registerEvents(new ReplaceBroken(), getInstance());
try {
@SuppressWarnings("unused")
ClickType swapOffhand = ClickType.SWAP_OFFHAND; //不是每个版本都有这个
getServer().getPluginManager().registerEvents(new InventoryF(), getInstance());
} catch (Throwable ignored) {
getLogger().warning("您的服务器不支持在玩家背包内按F整理,该功能已被禁用");
}
registerListener();
getLogger().info("已注册监听器");

getCommand("InvActions").setExecutor(new HandleCmd());
Expand Down Expand Up @@ -176,4 +121,80 @@ public static InvActions getInstance() {
return INSTANCE;
//
}

public boolean initCheck() {
if (!getServer().getPluginManager().isPluginEnabled("XPLib")) {
getLogger().severe("本插件需要XPLib作为前置...");
getLogger().severe("请在下载后,再加载本插件");
getLogger().severe("GitHub: https://github.com/0XPYEX0/XPLib/releases");
getLogger().severe("Gitee(国内): https://gitee.com/XPYEX/XPLib/releases");
return false;
}

if (VersionUtil.getMainVersion() < 9) {
getLogger().severe("本插件需要Minecraft至少为1.9才可运行");
getLogger().severe("很遗憾,您的服务器不满足此条件...");
return false;
}

if (!VersionUtil.requireXPLib(new Version(XPLIB_VER))) {
getLogger().severe("请更新您服务器内的XPLib!");
getLogger().severe("当前XPLib无法支持本插件");
getLogger().severe("需要: " + XPLIB_VER + " , 当前: " + VersionUtil.getXPLibVersion().getVersion());
getLogger().severe("GitHub: https://github.com/0XPYEX0/XPLib/releases");
getLogger().severe("Gitee(国内): https://gitee.com/XPYEX/XPLib/releases");
getServer().getPluginManager().disablePlugin(getInstance());
return false;
}

return true;
}

public void updateServerConfig() {
ConfigUtil.saveConfig(getInstance(), "config", GsonUtil.parseStr(SettingsUtil.DEFAULT_SERVER_SETTINGS), false);
JsonObject o = GsonUtil.copy(SettingsUtil.DEFAULT_SERVER_SETTINGS);
JsonObject config = ConfigUtil.getConfig(getInstance());
for (String key : GsonUtil.getKeysOfJsonObject(config)) {
o.add(key, config.get(key));
}
ConfigUtil.saveConfig(getInstance(), "config", GsonUtil.parseStr(o), true);
} //更新服务端config.json

public void updatePlayersConfig() {
getServer().getOnlinePlayers().forEach((player -> {
ConfigUtil.saveConfig(InvActions.getInstance(), "players/" + player.getUniqueId(), GsonUtil.parseStr(SettingsUtil.DEFAULT_SETTINGS), false);
//如果文件还不存在,则新建一份
JsonObject before = ConfigUtil.getConfig(InvActions.getInstance(), "players/" + player.getUniqueId());
JsonObject out = GsonUtil.copy(SettingsUtil.DEFAULT_SETTINGS);
for (String setting : GsonUtil.getKeysOfJsonObject(SettingsUtil.DEFAULT_SETTINGS)) {
if (before.has(setting)) {
out.add(setting, before.get(setting));
}
} //更新设定,应对热拔插
ConfigUtil.saveConfig(InvActions.getInstance(), "players/" + player.getUniqueId(), GsonUtil.parseStr(out), true);
}));
}

public void registerListener() {
getServer().getPluginManager().registerEvents(new AutoFarmer(), getInstance());
getServer().getPluginManager().registerEvents(new CraftDrop(), getInstance());
getServer().getPluginManager().registerEvents(new HandleEvent(), getInstance());
getServer().getPluginManager().registerEvents(new QuickDrop(), getInstance());
getServer().getPluginManager().registerEvents(new QuickMove(), getInstance());
getServer().getPluginManager().registerEvents(new ReplaceBroken(), getInstance());
try {
@SuppressWarnings("unused")
ClickType swapOffhand = ClickType.SWAP_OFFHAND; //1.16+
getServer().getPluginManager().registerEvents(new InventoryF(), getInstance());
} catch (Throwable ignored) {
getLogger().warning("您的服务器不支持在玩家背包内按F整理,该功能已被禁用");
}

try {
Block.class.getMethod("getBreakSpeed", Player.class); //1.17+
getServer().getPluginManager().registerEvents(new AutoTool(), getInstance());
} catch (NoSuchMethodError | NoSuchMethodException ignored) {
getLogger().warning("您的服务器不支持自动切换玩家工具,该功能已禁用");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}
if (sender instanceof Player) {
ConfigUtil.saveConfig(InvActions.getInstance(), "players/" + ((Player) sender).getUniqueId(), GsonUtil.parseStr(SettingsUtil.DEFAULT_SETTINGS), false);
InvSetter setter = new InvSetter("InvActions-设定-" + sender.getName(), "#########", "#1234567#", "#8 A#", "#########");
InvSetter setter = new InvSetter("InvActions-设定-" + sender.getName(), "#########", "#1234567#", "#89 A#", "#########");
setter.setSign("#", ItemUtil.getItemStack(Material.BLACK_STAINED_GLASS_PANE, " "));
setter.setSign(" ", Material.AIR);

Expand Down Expand Up @@ -200,13 +200,21 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
SettingsUtil.turnSetting(player, "DynamicLight");
}))
, 1)
.setSign("9", new UnmodifiableButton(menu, (player -> SettingsUtil.getMenuShow(player, "AutoTool")))
.addMode(-1, ItemUtil.getItemStack(Material.RED_WOOL, "&a自动更换工具", "&f当尝试破坏方块", "&f或尝试耕地时", "&f自动从背包内换出适合的工具", "", "&f当前状态: &4服务端禁用"))
.addMode(0, ItemUtil.getItemStack(Material.RED_WOOL, "&a自动更换工具", "&f当尝试破坏方块", "&f或尝试耕地时", "&f自动从背包内换出适合的工具", "", "&f当前状态: &c禁用"))
.addMode(1, ItemUtil.getItemStack(Material.LIME_WOOL, "&a自动更换工具", "&f当尝试破坏方块", "&f或尝试耕地时", "&f自动从背包内换出适合的工具", "", "&f当前状态: &a启用"))
.setClickEffect((player, clickType, itemStack) -> {
SettingsUtil.turnSetting(player, "AutoTool");
})
, 1)
.setSign("A", new UnmodifiableButton(menu, (player -> {
return player.hasPermission("InvActions.admin") ? 1 : 0;
}))
.addMode(1, ItemUtil.getItemStack(Material.ORANGE_WOOL, "&6重载所有配置文件", "&f包括服务端和玩家"))
.addMode(0, new ItemStack(Material.AIR))
.setClickEffect(((player, clickType, itemStack) -> {
if (itemStack.getAmount() == 0) {
if (itemStack.getAmount() == 0) { //玩家没权限的时候
return;
}
Bukkit.getScheduler().runTask(InvActions.getInstance(), () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public static boolean isAir(Material material) {
return material == Material.AIR;
}
}

public enum ToolType {
AXE,
PICKAXE,
SHOVEL,
HOE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.xpyex.plugin.invactions.bukkit.listener;

import java.util.HashMap;
import java.util.TreeMap;
import me.xpyex.plugin.invactions.bukkit.enums.ItemType;
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 me.xpyex.plugin.xplib.bukkit.util.strings.StrUtil;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

public class AutoTool implements Listener {
private static final HashMap<ItemType.ToolType, ItemStack> TOOLS = new HashMap<>();
static {
TOOLS.put(ItemType.ToolType.AXE, new ItemStack(Material.DIAMOND_AXE));
TOOLS.put(ItemType.ToolType.PICKAXE, new ItemStack(Material.DIAMOND_PICKAXE));
TOOLS.put(ItemType.ToolType.SHOVEL, new ItemStack(Material.DIAMOND_SHOVEL));
TOOLS.put(ItemType.ToolType.HOE, new ItemStack(Material.DIAMOND_HOE)); //割稻草块
}

@EventHandler
public void onInteract(PlayerInteractEvent event) {
if (!SettingsUtil.getServerSetting("AutoTool")) return;

if (!SettingsUtil.getSetting(event.getPlayer(), "AutoTool")) return;

if (event.getClickedBlock() != null) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (ItemUtil.typeIsOr(event.getClickedBlock(), Material.GRASS_BLOCK, Material.DIRT, Material.COARSE_DIRT)) {
if (StrUtil.containsIgnoreCaseOr(event.getPlayer().getInventory().getItemInMainHand().getType().toString(), "_SHOVEL", "_HOE"))
return; //玩家已经拿着对应道具了,就不要换
InvUtil.swapSlotToMainHand(event.getPlayer(), InvUtil.getFastestToolSlot(event.getPlayer(), event.getClickedBlock(), ItemType.ToolType.HOE));
}
}
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
ItemStack before = new ItemStack(event.getPlayer().getInventory().getItemInMainHand()); //保存主手
TreeMap<Float, ItemType.ToolType> timeCost = new TreeMap<>();
for (ItemType.ToolType toolType : TOOLS.keySet()) {
ItemStack item = TOOLS.get(toolType);
event.getPlayer().getInventory().setItemInMainHand(item); //手中设为模板
timeCost.put(event.getClickedBlock().getBreakSpeed(event.getPlayer()), toolType); //计算并排序
}
event.getPlayer().getInventory().setItemInMainHand(before); //恢复手中道具
if (timeCost.size() <= 1) { //时间都一样,或者根本没算成,无需更换道具
return;
}
InvUtil.swapSlotToMainHand(event.getPlayer(), InvUtil.getFastestToolSlot(event.getPlayer(), event.getClickedBlock(), timeCost.get(timeCost.lastKey()))); //更换这个类别中最快速度的工具
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void onPressFWithoutInv(PlayerSwapHandItemsEvent event) {
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.UI_BUTTON_CLICK, 1f, 1f);
return;
}
if (!SettingsUtil.getSetting(event.getPlayer(), "DefaultF") && !event.getPlayer().isSneaking()) {
if (!SettingsUtil.getSetting(event.getPlayer(), "DefaultF") && !(event.getPlayer().isSneaking() && event.getPlayer().getLocation().getPitch() == 90)) {
return;
}
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void onPressFWithInv(InventoryClickEvent event) {
if (event.getClickedInventory() == null) {
return;
}
if (event.getClick() == ClickType.SWAP_OFFHAND) { //不知道哪个版本加上的
if (event.getClick() == ClickType.SWAP_OFFHAND) {
if (event.getClickedInventory() == event.getWhoClicked().getInventory() || InvUtil.isNotMenu(event.getClickedInventory())) { //仅允许整理自己的背包,或是方块的界面,不允许整理菜单
event.setCancelled(true);
SortUtil.sortInv(event.getClickedInventory());
Expand Down
Loading

0 comments on commit bfcbe80

Please sign in to comment.