Skip to content

Commit

Permalink
大幅优化结构
Browse files Browse the repository at this point in the history
  • Loading branch information
0XPYEX0 committed Feb 11, 2022
1 parent b8c1f52 commit 5d3a702
Show file tree
Hide file tree
Showing 38 changed files with 363 additions and 343 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = 'me.xpyex'
version = '1.3.1'
version = '1.3.2'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/me/xpyex/plugin/flywithfood/bukkit/FlyWithFood.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package me.xpyex.plugin.flywithfood.bukkit;

import java.util.logging.Logger;

import me.xpyex.plugin.flywithfood.bukkit.commands.FlyCmd;
import me.xpyex.plugin.flywithfood.bukkit.config.HandleConfig;
import me.xpyex.plugin.flywithfood.bukkit.events.FWFPlayerBeenDisableFlyEvent;
import me.xpyex.plugin.flywithfood.bukkit.implementations.FWFUser;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.EXPLevelEnergy;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.EXPPointEnergy;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.FlyEnergy;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.FoodEnergy;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.MoneyEnergy;
import me.xpyex.plugin.flywithfood.bukkit.implementations.BukkitUser;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.BukkitExpLevel;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.BukkitExpPoint;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.BukkitFood;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.BukkitMoney;
import me.xpyex.plugin.flywithfood.bukkit.utils.VersionUtil;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.FlyEnergy;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.energys.FoodEnergy;
import me.xpyex.plugin.flywithfood.common.networks.NetWorkUtil;
import me.xpyex.plugin.flywithfood.common.types.FWFMsgType;

import net.milkbowl.vault.economy.Economy;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
Expand All @@ -43,11 +41,11 @@ public void onEnable() {
getCommand("FlyWithFood").setExecutor(new FlyCmd());

{
new EXPPointEnergy().register();
new EXPLevelEnergy().register();
new FoodEnergy().register();
new BukkitExpPoint().register();
new BukkitExpLevel().register();
new BukkitFood().register();
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
new MoneyEnergy().register();
new BukkitMoney().register();

RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp != null) {
Expand Down Expand Up @@ -120,7 +118,7 @@ public void onDisable() {
public static void startCheck() {
Bukkit.getScheduler().runTaskTimerAsynchronously(INSTANCE, () -> {
for (Player player : Bukkit.getOnlinePlayers()) {
FWFUser user = new FWFUser(player);
BukkitUser user = new BukkitUser(player);
if (!user.needCheck()) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import me.xpyex.plugin.flywithfood.bukkit.events.FWFDisableFlyEvent;
import me.xpyex.plugin.flywithfood.bukkit.events.FWFEnableFlyEvent;
import me.xpyex.plugin.flywithfood.bukkit.events.FWFPlayerBeenDenyCmdEvent;
import me.xpyex.plugin.flywithfood.bukkit.implementations.FWFUser;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.FoodEnergy;
import me.xpyex.plugin.flywithfood.bukkit.implementations.BukkitUser;
import me.xpyex.plugin.flywithfood.bukkit.utils.Utils;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.energys.FoodEnergy;
import me.xpyex.plugin.flywithfood.common.types.DenyReason;
import me.xpyex.plugin.flywithfood.common.types.FWFMsgType;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -111,7 +111,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
Utils.sendFWFMsg(sender, FWFMsgType.PlayerNotOnline);
return true;
}
FWFUser targetUser = new FWFUser(target);
BukkitUser targetUser = new BukkitUser(target);
if ((target != sender && !sender.hasPermission("fly.other"))
||
(target == sender && !sender.hasPermission("fly.fly"))) {
Expand All @@ -138,7 +138,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}
if (args[0].equalsIgnoreCase("on")) {
if (targetUser.getInfo().getEnergy() instanceof FoodEnergy) {
if (targetUser.hasSaturationEff() && !targetUser.nocost()) {
if (targetUser.hasSaturationEff() && !targetUser.hasPermission()) {
FWFPlayerBeenDenyCmdEvent event = new FWFPlayerBeenDenyCmdEvent(target, DenyReason.HasEffect, "on");
Bukkit.getPluginManager().callEvent(event);
if (target != sender) {
Expand All @@ -153,7 +153,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
return true;
}
}
if ((target.getFoodLevel() < targetUser.getInfo().getDisable()) && !targetUser.nocost()) {
if ((target.getFoodLevel() < targetUser.getInfo().getDisable()) && !targetUser.hasPermission()) {
FWFPlayerBeenDenyCmdEvent event = new FWFPlayerBeenDenyCmdEvent(target, DenyReason.NotEnoughFood, "on");
Bukkit.getPluginManager().callEvent(event);
if (target != sender) {
Expand Down Expand Up @@ -185,7 +185,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
}
if (args[0].equalsIgnoreCase("off")) {
FWFDisableFlyEvent event = new FWFDisableFlyEvent(target);
new FWFUser(target).disableFly(event);
new BukkitUser(target).disableFly(event);
Utils.sendFWFMsg(target, FWFMsgType.DisableFly);
if (target != sender) {
if (HandleConfig.config.isChinese) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,28 @@

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import me.xpyex.plugin.flywithfood.bukkit.FlyWithFood;
import me.xpyex.plugin.flywithfood.bukkit.events.FWFReloadConfigEvent;
import me.xpyex.plugin.flywithfood.bukkit.implementations.energys.EnergyManager;
import me.xpyex.plugin.flywithfood.bukkit.reflections.NMSAll;
import me.xpyex.plugin.flywithfood.bukkit.utils.VersionUtil;
import me.xpyex.plugin.flywithfood.common.config.ConfigUtil;
import me.xpyex.plugin.flywithfood.common.config.FWFConfig;

import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.EnergyManager;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.nio.charset.StandardCharsets;

public class HandleConfig {
public static FWFConfig config;
private final static File ROOT = new File("plugins/" + FlyWithFood.INSTANCE.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
import me.xpyex.plugin.flywithfood.bukkit.runnables.DisableFly;
import me.xpyex.plugin.flywithfood.bukkit.runnables.FallRunnable;
import me.xpyex.plugin.flywithfood.bukkit.utils.Utils;
import me.xpyex.plugin.flywithfood.common.implementations.FWFInfo;
import me.xpyex.plugin.flywithfood.common.implementations.FWFUser;
import me.xpyex.plugin.flywithfood.common.types.FWFMsgType;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;

public class FWFUser {
public class BukkitUser implements FWFUser {
private final Player player;

public FWFUser(Player p) {
public BukkitUser(Player p) {
this.player = p;
}

public void autoSendMsg(String... msg) {
Utils.autoSendMsg(player, msg);
}

public void sendFWFMsg(FWFMsgType msg) {
Utils.sendFWFMsg(player, msg);
}
Expand All @@ -25,7 +31,7 @@ public boolean hasSaturationEff() {
}

public void cost(double value) {
this.getInfo().getEnergy().cost(player, value);
this.getInfo().getEnergy().cost(this, value);
}

public void disableFly() {
Expand Down Expand Up @@ -55,12 +61,12 @@ public FWFInfo getInfo() {
return new FWFInfo(cost, disable, mode);
}

public boolean nocost() {
public boolean hasPermission() {
return (player.hasPermission("fly.nohunger") || player.hasPermission("fly.nocost"));
}

public Number getNow() {
return getInfo().getEnergy().getNow(player);
return getInfo().getEnergy().getNow(this);
}

public boolean inNoCost() { //玩家所在的世界是否不消耗
Expand All @@ -86,7 +92,7 @@ public boolean needCheck() {
if ("CREATIVE, SPECTATOR".contains(player.getGameMode().toString())) { //1.7没有旁观者模式,创造模式与旁观者模式没有处理的必要
return false;
}
if (nocost()) { //若玩家拥有权限无视消耗,则没有处理的必要
if (hasPermission()) { //若玩家拥有权限无视消耗,则没有处理的必要
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package me.xpyex.plugin.flywithfood.bukkit.implementations.energys;

import me.xpyex.plugin.flywithfood.bukkit.FlyWithFood;

import me.xpyex.plugin.flywithfood.common.implementations.FWFUser;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.EnergyManager;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.energys.ExpLevelEnergy;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class EXPLevelEnergy implements FlyEnergy {
public class BukkitExpLevel implements ExpLevelEnergy {
@Override
public @NotNull String getName() {
return "ExpLevel";
}

@Override
public void cost(@NotNull Player target, @NotNull Number value) {
public void cost(@NotNull FWFUser user, @NotNull Number value) {
Player target = (Player) user.getPlayer();
Bukkit.getScheduler().runTask(FlyWithFood.INSTANCE, () ->
target.setLevel(Math.max(target.getLevel() - value.intValue(), 0))
);
}

@Override
public @NotNull Integer getNow(Player target) {
public @NotNull Integer getNow(FWFUser user) {
Player target = (Player) user.getPlayer();
return target.getLevel();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.xpyex.plugin.flywithfood.bukkit.implementations.energys;

import me.xpyex.plugin.flywithfood.bukkit.FlyWithFood;
import me.xpyex.plugin.flywithfood.common.implementations.FWFUser;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.EnergyManager;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.energys.ExpPointEnergy;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class BukkitExpPoint implements ExpPointEnergy {
@Override
public @NotNull String getName() {
return "ExpPoint";
}

@Override
public void cost(@NotNull FWFUser user, @NotNull Number value) {
Player target = (Player) user.getPlayer();
if (value.floatValue() < -1f || value.floatValue() > 1f) {
throw new IllegalArgumentException("在" + getName() + "模式中,Cost项∈[-1.0, 1.0].小数位可精确到6位.当前为" + value.floatValue() + ", 您也可尝试" + new BukkitExpLevel().getName() + "模式");
}
if (target.getExp() - value.floatValue() < 0f) {
Bukkit.getScheduler().runTask(FlyWithFood.INSTANCE, () -> {
target.setLevel(target.getLevel() - 1);
target.setExp(1f + target.getExp() - value.floatValue());
});
} else if (target.getExp() - value.floatValue() > 1f) {
Bukkit.getScheduler().runTask(FlyWithFood.INSTANCE, () -> {
target.setLevel(target.getLevel() + 1);
target.setExp(target.getExp() - value.floatValue() - 1f);
});
} else {
Bukkit.getScheduler().runTask(FlyWithFood.INSTANCE, () ->
target.setExp(target.getExp() - value.floatValue())
);
}
}

@Override
public @NotNull Float getNow(FWFUser user) {
Player target = (Player) user.getPlayer();
return target.getExp();
}

@Override
public void register() {
EnergyManager.registerEnergy(getName(), this);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
package me.xpyex.plugin.flywithfood.bukkit.implementations.energys;

import me.xpyex.plugin.flywithfood.bukkit.FlyWithFood;

import me.xpyex.plugin.flywithfood.common.implementations.FWFUser;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.EnergyManager;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.energys.FoodEnergy;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class FoodEnergy implements FlyEnergy {
public class BukkitFood implements FoodEnergy {
@Override
public @NotNull String getName() {
return "Food";
}

@Override
public void cost(@NotNull Player target, @NotNull Number value) {
public void cost(@NotNull FWFUser user, @NotNull Number value) {
Player target = (Player) user.getPlayer();
Bukkit.getScheduler().runTask(FlyWithFood.INSTANCE, () ->
target.setFoodLevel(Math.max(target.getFoodLevel() - value.intValue(), 0))
);
}

@Override
public @NotNull Integer getNow(Player target) {
public @NotNull Integer getNow(FWFUser user) {
Player target = (Player) user.getPlayer();
return target.getFoodLevel();
}

@Override
public void register() {
EnergyManager.registerEnergy(getName(), this);
//
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.xpyex.plugin.flywithfood.bukkit.implementations.energys;

import me.xpyex.plugin.flywithfood.bukkit.FlyWithFood;
import me.xpyex.plugin.flywithfood.common.implementations.FWFUser;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.EnergyManager;
import me.xpyex.plugin.flywithfood.common.implementations.flyenergy.energys.MoneyEnergy;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class BukkitMoney implements MoneyEnergy {
@Override
public @NotNull String getName() {
return "Money";
}

@Override
public void cost(@NotNull FWFUser user, @NotNull Number value) {
Player target = (Player) user.getPlayer();
if (FlyWithFood.econ != null) {
FlyWithFood.econ.depositPlayer(target, getNow(user) - value.doubleValue());
}
}

@Override
public @NotNull Double getNow(FWFUser user) {
Player target = (Player) user.getPlayer();
return FlyWithFood.econ.getBalance(target);
}

@Override
public void register() {
EnergyManager.registerEnergy(getName(), this);
}
}
Loading

0 comments on commit 5d3a702

Please sign in to comment.