current = meta.getLore();
227 | if (current == null) current = new ArrayList<>();
228 | current.addAll(lore);
229 | meta.setLore(current);
230 | stack.setItemMeta(meta);
231 | return this;
232 | }
233 |
234 | public ItemFactory flag(String flag) {
235 | if (Sounds.version == 17) return this;
236 | ItemMeta meta = stack.getItemMeta();
237 | meta.addItemFlags(org.bukkit.inventory.ItemFlag.valueOf(flag));
238 | stack.setItemMeta(meta);
239 | return this;
240 | }
241 |
242 | public ItemFactory customModelData(int customModelData) {
243 | if (Sounds.version < 114) return this;
244 | ItemMeta meta = stack.getItemMeta();
245 | ItemUtils1_14.applyCustomModelData(meta, customModelData);
246 | stack.setItemMeta(meta);
247 | return this;
248 | }
249 |
250 | public int getAmount() {
251 | return stack.getAmount();
252 | }
253 | }
254 |
--------------------------------------------------------------------------------
/src/main/java/com/trophonix/tradeplus/util/ItemUtils1_14.java:
--------------------------------------------------------------------------------
1 | package com.trophonix.tradeplus.util;
2 |
3 | import org.bukkit.NamespacedKey;
4 | import org.bukkit.enchantments.Enchantment;
5 | import org.bukkit.inventory.ItemStack;
6 | import org.bukkit.inventory.meta.ItemMeta;
7 |
8 | public class ItemUtils1_14 {
9 |
10 | public static int getCustomModelData(ItemStack stack) {
11 | if (!stack.hasItemMeta()) return 0;
12 | return stack.getItemMeta().getCustomModelData();
13 | }
14 |
15 | public static void applyCustomModelData(ItemMeta meta, int customModelData) {
16 | meta.setCustomModelData(customModelData);
17 | }
18 |
19 | public static String getName(Enchantment enchantment) {
20 | return enchantment.getKey().getKey();
21 | }
22 |
23 | public static Enchantment getEnchantment(String key) {
24 | return Enchantment.getByKey(NamespacedKey.minecraft(key));
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/trophonix/tradeplus/util/MsgUtils.java:
--------------------------------------------------------------------------------
1 | package com.trophonix.tradeplus.util;
2 |
3 | import org.bukkit.ChatColor;
4 | import org.bukkit.command.CommandSender;
5 | import org.bukkit.entity.Player;
6 |
7 | import java.util.regex.Matcher;
8 | import java.util.regex.Pattern;
9 |
10 | import static org.bukkit.ChatColor.COLOR_CHAR;
11 |
12 | public class MsgUtils {
13 |
14 | private static boolean clickableMessages;
15 |
16 | static {
17 | try {
18 | clickableMessages = Class.forName("net.md_5.bungee.api.chat.TextComponent") != null;
19 | } catch (ClassNotFoundException ignored) {
20 | clickableMessages = false;
21 | }
22 | }
23 |
24 | public static void send(Player player, String onHover, String onClick, String[] messages) {
25 | if (clickableMessages) {
26 | MsgUtils1_8.send(player, onHover, onClick, messages);
27 | } else {
28 | send(player, messages);
29 | }
30 | }
31 |
32 | public static void send(Player player, String onHover, String onClick, String message) {
33 | if (message.contains("%NEWLINE%")) {
34 | send(player, onHover, onClick, message.split("%NEWLINE%"));
35 | } else {
36 | send(player, onHover, onClick, new String[] {message});
37 | }
38 | }
39 |
40 | public static void send(CommandSender sender, String[] messages) {
41 | for (String message : messages) {
42 | send(sender, message);
43 | }
44 | }
45 |
46 | public static void send(CommandSender sender, String message) {
47 | sender.sendMessage(color(message));
48 | }
49 |
50 | public static final String startTag = "&\\{";
51 | public static final String endTag = "}";
52 |
53 | public static String color(String string) {
54 | final Pattern hexPattern = Pattern.compile(startTag + "([A-Fa-f0-9]{6})" + endTag);
55 | Matcher matcher = hexPattern.matcher(string);
56 | StringBuffer buffer = new StringBuffer(string.length() + 4 * 8);
57 | while (matcher.find())
58 | {
59 | String group = matcher.group(1);
60 | matcher.appendReplacement(buffer, COLOR_CHAR + "x"
61 | + COLOR_CHAR + group.charAt(0) + COLOR_CHAR + group.charAt(1)
62 | + COLOR_CHAR + group.charAt(2) + COLOR_CHAR + group.charAt(3)
63 | + COLOR_CHAR + group.charAt(4) + COLOR_CHAR + group.charAt(5)
64 | );
65 | }
66 | return ChatColor.translateAlternateColorCodes('&', matcher.appendTail(buffer).toString());
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/com/trophonix/tradeplus/util/MsgUtils1_8.java:
--------------------------------------------------------------------------------
1 | package com.trophonix.tradeplus.util;
2 |
3 | import net.md_5.bungee.api.chat.BaseComponent;
4 | import net.md_5.bungee.api.chat.ClickEvent;
5 | import net.md_5.bungee.api.chat.HoverEvent;
6 | import net.md_5.bungee.api.chat.TextComponent;
7 | import org.bukkit.ChatColor;
8 | import org.bukkit.entity.Player;
9 |
10 | class MsgUtils1_8 {
11 |
12 | public static void send(Player player, String onHover, String onClick, String[] messages) {
13 | for (String m : messages) {
14 | BaseComponent[] comps =
15 | TextComponent.fromLegacyText(MsgUtils.color(m));
16 | for (BaseComponent comp : comps) {
17 | if (onHover != null)
18 | comp.setHoverEvent(
19 | new HoverEvent(
20 | HoverEvent.Action.SHOW_TEXT,
21 | TextComponent.fromLegacyText(
22 | MsgUtils.color(onHover))));
23 | if (onClick != null)
24 | comp.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, onClick));
25 | }
26 | player.spigot().sendMessage(comps);
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/trophonix/tradeplus/util/NMSManager.java:
--------------------------------------------------------------------------------
1 | package com.trophonix.tradeplus.util;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.entity.Player;
5 |
6 | import java.lang.reflect.Field;
7 | import java.lang.reflect.InvocationTargetException;
8 | import java.lang.reflect.Method;
9 | import java.util.HashMap;
10 | import java.util.Map;
11 |
12 | /**
13 | * Created by PhilipsNostrum
14 | *
15 | * Cleaned up & useNewVersion-added by Gecolay
16 | */
17 | public class NMSManager {
18 |
19 | public static final Map, Class>> CORRESPONDING_TYPES =
20 | new HashMap, Class>>();
21 |
22 | public static Class> getPrimitiveType(Class> Class) {
23 | return CORRESPONDING_TYPES.containsKey(Class) ? CORRESPONDING_TYPES.get(Class) : Class;
24 | }
25 |
26 | public static Class>[] toPrimitiveTypeArray(Class>[] Classes) {
27 | int L = Classes != null ? Classes.length : 0;
28 | Class>[] T = new Class>[L];
29 | for (int i = 0; i < L; i++) T[i] = getPrimitiveType(Classes[i]);
30 | return T;
31 | }
32 |
33 | public static boolean equalsTypeArray(Class>[] Value1, Class>[] Value2) {
34 | if (Value1.length != Value2.length) return false;
35 | for (int i = 0; i < Value1.length; i++)
36 | if (!Value1[i].equals(Value2[i]) && !Value1[i].isAssignableFrom(Value2[i])) return false;
37 | return true;
38 | }
39 |
40 | public static boolean classListEqual(Class>[] Value1, Class>[] Value2) {
41 | if (Value1.length != Value2.length) return false;
42 | for (int i = 0; i < Value1.length; i++) if (Value1[i] != Value2[i]) return false;
43 | return true;
44 | }
45 |
46 | public static String getVersion() {
47 | String V = Bukkit.getServer().getClass().getPackage().getName();
48 | return V.substring(V.lastIndexOf('.') + 1) + ".";
49 | }
50 |
51 | public static boolean useNewVersion() {
52 | try {
53 | Class.forName("net.minecraft.server." + getVersion() + "ContainerAccess");
54 | return true;
55 | } catch (Exception e) {
56 | return false;
57 | }
58 | }
59 |
60 | public static Field getField(Class> Class, String Field) {
61 | try {
62 | Field F = Class.getDeclaredField(Field);
63 | F.setAccessible(true);
64 | return F;
65 | } catch (Exception e) {
66 | e.printStackTrace();
67 | return null;
68 | }
69 | }
70 |
71 | public static Class> getNMSClass(String ClassName) {
72 | Class> C = null;
73 | try {
74 | return Class.forName("net.minecraft.server." + getVersion() + ClassName);
75 | } catch (Exception e) {
76 | e.printStackTrace();
77 | }
78 | return C;
79 | }
80 |
81 | public static Method getMethod(Class> Class, String ClassName, Class>... Parameters) {
82 | for (Method M : Class.getMethods())
83 | if (M.getName().equals(ClassName)
84 | && (Parameters.length == 0 || classListEqual(Parameters, M.getParameterTypes()))) {
85 | M.setAccessible(true);
86 | return M;
87 | }
88 | return null;
89 | }
90 |
91 | public static Method getMethod(String MethodName, Class> Class, Class>... Parameters) {
92 | Class>[] T = toPrimitiveTypeArray(Parameters);
93 | for (Method M : Class.getMethods())
94 | if (M.getName().equals(MethodName)
95 | && equalsTypeArray(toPrimitiveTypeArray(M.getParameterTypes()), T)) return M;
96 | return null;
97 | }
98 |
99 | public static Object getHandle(Object Object) {
100 | try {
101 | return getMethod("getHandle", Object.getClass()).invoke(Object);
102 | } catch (Exception e) {
103 | e.printStackTrace();
104 | return null;
105 | }
106 | }
107 |
108 | public static Object getPlayerField(Player Player, String Field)
109 | throws SecurityException, NoSuchMethodException, NoSuchFieldException,
110 | IllegalArgumentException, IllegalAccessException, InvocationTargetException {
111 | Object P = Player.getClass().getMethod("getHandle").invoke(Player);
112 | return P.getClass().getField(Field).get(P);
113 | }
114 |
115 | public static Object invokeMethod(String MethodName, Object Parameter) {
116 | try {
117 | return getMethod(MethodName, Parameter.getClass()).invoke(Parameter);
118 | } catch (Exception e) {
119 | e.printStackTrace();
120 | return null;
121 | }
122 | }
123 |
124 | public static Object invokeMethodWithArgs(
125 | String MethodName, Object Object, Object... Parameters) {
126 | try {
127 | return getMethod(MethodName, Object.getClass()).invoke(Object, Parameters);
128 | } catch (Exception e) {
129 | e.printStackTrace();
130 | return null;
131 | }
132 | }
133 |
134 | public static boolean set(Object Object, String Field, Object Value) {
135 | Class> C = Object.getClass();
136 | while (C != null) {
137 | try {
138 | Field F = C.getDeclaredField(Field);
139 | F.setAccessible(true);
140 | F.set(Object, Value);
141 | return true;
142 | } catch (NoSuchFieldException e) {
143 | C = C.getSuperclass();
144 | } catch (Exception e) {
145 | throw new IllegalStateException(e);
146 | }
147 | }
148 | return false;
149 | }
150 | }
151 |
--------------------------------------------------------------------------------
/src/main/java/com/trophonix/tradeplus/util/PDCUtils.java:
--------------------------------------------------------------------------------
1 | package com.trophonix.tradeplus.util;
2 |
3 | import com.trophonix.tradeplus.TradePlus;
4 | import org.bukkit.NamespacedKey;
5 | import org.bukkit.entity.Player;
6 | import org.bukkit.persistence.PersistentDataContainer;
7 | import org.bukkit.persistence.PersistentDataType;
8 |
9 | public class PDCUtils {
10 |
11 | private static NamespacedKey ALLOW_TRADING;
12 |
13 | public static boolean allowTrading(Player player) {
14 | PersistentDataContainer pdc = player.getPersistentDataContainer();
15 | return pdc.getOrDefault(ALLOW_TRADING, PersistentDataType.BYTE, (byte)1) == 1;
16 | }
17 |
18 | public static boolean toggleTrading(Player player) {
19 | PersistentDataContainer pdc = player.getPersistentDataContainer();
20 | boolean allow = allowTrading(player);
21 | pdc.set(ALLOW_TRADING, PersistentDataType.BYTE, allow ? (byte)0 : (byte)1);
22 | return !allow;
23 | }
24 |
25 | public static void initialize(TradePlus plugin) {
26 | ALLOW_TRADING = new NamespacedKey(plugin, "allow_trading");
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/trophonix/tradeplus/util/PlayerUtil.java:
--------------------------------------------------------------------------------
1 | package com.trophonix.tradeplus.util;
2 |
3 | import com.trophonix.tradeplus.TradePlus;
4 | import com.trophonix.tradeplus.hooks.EssentialsHook;
5 | import org.bukkit.Bukkit;
6 | import org.bukkit.entity.Player;
7 | import org.bukkit.metadata.MetadataValue;
8 |
9 | import java.net.InetSocketAddress;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 | import java.util.Objects;
13 | import java.util.UUID;
14 |
15 | public class PlayerUtil {
16 |
17 | private static final Map ipAddresses = new HashMap<>();
18 |
19 | public static void registerIP(Player player) {
20 | InetSocketAddress address = player.getAddress();
21 | if (address != null) {
22 | String ip = player.getAddress().getHostString();
23 | if (ip != null) ipAddresses.put(player.getUniqueId(), ip);
24 | }
25 | }
26 |
27 | public static void removeIP(Player player) {
28 | ipAddresses.remove(player.getUniqueId());
29 | }
30 |
31 | public static boolean sameIP(Player player1, Player player2) {
32 | String ip1 = ipAddresses.get(player1.getUniqueId());
33 | String ip2 = ipAddresses.get(player2.getUniqueId());
34 | if (ip1 == null || ip2 == null) return false;
35 | return ip1.equals(ip2);
36 | }
37 |
38 | public static boolean isVanished(Player player) {
39 | if (Bukkit.getPluginManager().isPluginEnabled("Essentials")) {
40 | if (EssentialsHook.isVanished(player)) return true;
41 | }
42 |
43 | for (MetadataValue meta : player.getMetadata("vanished")) {
44 | if (meta.asBoolean()) return true;
45 | }
46 | return false;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/com/trophonix/tradeplus/util/Procedure.java:
--------------------------------------------------------------------------------
1 | package com.trophonix.tradeplus.util;
2 |
3 | public interface Procedure {
4 | void invoke();
5 | }
6 |
--------------------------------------------------------------------------------
/src/main/java/com/trophonix/tradeplus/util/Sounds.java:
--------------------------------------------------------------------------------
1 | package com.trophonix.tradeplus.util;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.ChatColor;
5 | import org.bukkit.Sound;
6 | import org.bukkit.entity.Player;
7 |
8 | public class Sounds {
9 |
10 | public static final int version;
11 | private static Sound pling;
12 | private static Sound click;
13 | private static Sound levelUp;
14 | private static Sound villagerHit;
15 | private static Sound villagerHmm;
16 |
17 | static {
18 | String[] split =
19 | Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3].split("_");
20 | version = Integer.parseInt(split[0].replace("v", "") + split[1]);
21 | // System.out.println("You appear to be running version " + version);
22 | }
23 |
24 | public static void loadSounds() {
25 | try {
26 | if (version < 19) {
27 | pling = Sound.valueOf("NOTE_PLING");
28 | click = Sound.valueOf("CLICK");
29 | levelUp = Sound.valueOf("LEVEL_UP");
30 | villagerHit = Sound.valueOf("VILLAGER_HIT");
31 | villagerHmm = Sound.valueOf("VILLAGER_IDLE");
32 | } else if (version < 113) {
33 | pling = Sound.valueOf("BLOCK_NOTE_PLING");
34 | click = Sound.valueOf("UI_BUTTON_CLICK");
35 | levelUp = Sound.valueOf("ENTITY_PLAYER_LEVELUP");
36 | villagerHit = Sound.valueOf("ENTITY_VILLAGER_HURT");
37 | villagerHmm = Sound.valueOf("ENTITY_VILLAGER_AMBIENT");
38 | } else {
39 | pling = Sound.valueOf("BLOCK_NOTE_BLOCK_PLING");
40 | click = Sound.valueOf("UI_BUTTON_CLICK");
41 | levelUp = Sound.valueOf("ENTITY_PLAYER_LEVELUP");
42 | villagerHit = Sound.valueOf("ENTITY_VILLAGER_HURT");
43 | villagerHmm = Sound.valueOf("ENTITY_VILLAGER_AMBIENT");
44 | }
45 | } catch (IllegalArgumentException | NullPointerException | NoSuchFieldError ex) {
46 | Bukkit.getConsoleSender()
47 | .sendMessage(
48 | ChatColor.DARK_RED + "Unable to load sounds! Sound effects will be disabled.");
49 | }
50 | }
51 |
52 | public static void pling(Player player, float v1) {
53 | if (pling != null) player.playSound(player.getEyeLocation(), pling, 1, v1);
54 | }
55 |
56 | public static void click(Player player, float v1) {
57 | if (click != null) player.playSound(player.getEyeLocation(), click, 1, v1);
58 | }
59 |
60 | public static void levelUp(Player player, float v1) {
61 | if (levelUp != null) player.playSound(player.getEyeLocation(), levelUp, 1, v1);
62 | }
63 |
64 | public static void villagerHit(Player player, float v1) {
65 | if (villagerHit != null) player.playSound(player.getEyeLocation(), villagerHit, 1, v1);
66 | }
67 |
68 | public static void villagerHmm(Player player, float v1) {}
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/com/trophonix/tradeplus/util/XP.java:
--------------------------------------------------------------------------------
1 | package com.trophonix.tradeplus.util;
2 |
3 | import org.bukkit.entity.Player;
4 |
5 | public class XP {
6 |
7 | /**
8 | * Calculates a player's total exp based on level and progress to next.
9 | * http://minecraft.gamepedia.com/Experience#Leveling_up
10 | *
11 | * @param player the Player
12 | * @return the amount of exp the Player has
13 | */
14 | public static int getExp(Player player) {
15 | return getExpFromLevel(player.getLevel())
16 | + Math.round(getExpToNext(player.getLevel()) * player.getExp());
17 | }
18 |
19 | /**
20 | * Calculates total experience based on level.
21 | *
22 | * http://minecraft.gamepedia.com/Experience#Leveling_up
23 | *
24 | *
"One can determine how much experience has been collected to reach a level using the
25 | * equations:
26 | *
27 | *
Total Experience = [Level]2 + 6[Level] (at levels 0-15) 2.5[Level]2 - 40.5[Level] + 360 (at
28 | * levels 16-30) 4.5[Level]2 - 162.5[Level] + 2220 (at level 31+)"
29 | *
30 | * @param level the level
31 | * @return the total experience calculated
32 | */
33 | public static int getExpFromLevel(int level) {
34 | if (level > 30) {
35 | return (int) (4.5 * level * level - 162.5 * level + 2220);
36 | }
37 | if (level > 15) {
38 | return (int) (2.5 * level * level - 40.5 * level + 360);
39 | }
40 | return level * level + 6 * level;
41 | }
42 |
43 | /**
44 | * Calculates level based on total experience.
45 | *
46 | * @param exp the total experience
47 | * @return the level calculated
48 | */
49 | public static double getLevelFromExp(long exp) {
50 | if (exp > 1395) {
51 | return (Math.sqrt(72 * exp - 54215) + 325) / 18;
52 | }
53 | if (exp > 315) {
54 | return Math.sqrt(40 * exp - 7839) / 10 + 8.1;
55 | }
56 | if (exp > 0) {
57 | return Math.sqrt(exp + 9) - 3;
58 | }
59 | return 0;
60 | }
61 |
62 | /**
63 | * http://minecraft.gamepedia.com/Experience#Leveling_up
64 | *
65 | *
"The formulas for figuring out how many experience orbs you need to get to the next level
66 | * are as follows: Experience Required = 2[Current Level] + 7 (at levels 0-15) 5[Current Level] -
67 | * 38 (at levels 16-30) 9[Current Level] - 158 (at level 31+)"
68 | */
69 | private static int getExpToNext(int level) {
70 | if (level > 30) {
71 | return 9 * level - 158;
72 | }
73 | if (level > 15) {
74 | return 5 * level - 38;
75 | }
76 | return 2 * level + 7;
77 | }
78 |
79 | /**
80 | * Change a Player's exp.
81 | *
82 | *
This method should be used in place of {@link Player#giveExp(int)}, which does not properly
83 | * account for different levels requiring different amounts of experience.
84 | *
85 | * @param player the Player affected
86 | * @param exp the amount of experience to add or remove
87 | */
88 | public static void changeExp(Player player, int exp) {
89 | exp += getExp(player);
90 |
91 | if (exp < 0) {
92 | exp = 0;
93 | }
94 |
95 | double levelAndExp = getLevelFromExp(exp);
96 |
97 | int level = (int) levelAndExp;
98 | player.setLevel(level);
99 | player.setExp((float) (levelAndExp - level));
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/src/main/resources/plugin.yml:
--------------------------------------------------------------------------------
1 | name: TradePlus
2 | version: ${project.version}
3 | author: Trophonix
4 | main: com.trophonix.tradeplus.TradePlus
5 | softdepend: [Vault,EnjinMinecraftPlugin,GriefPrevention,PlayerPoints,TokenManager,BeastTokens,TokenEnchant,WorldGuard,VotingPlugin]
6 | api-version: "1.16"
7 | commands:
8 | trade:
9 | description: Trade command
10 | tradeplus:
11 | description: TradePlus admin command
12 | permission: tradeplus.admin
13 | permissions:
14 | tradeplus.admin:
15 | description: TradePlus admin permission
16 | default: op
17 | children:
18 | tradeplus.trade: true
19 | tradeplus.admin.silent:
20 | description: Silence admin trade notifications
21 | default: false
--------------------------------------------------------------------------------