Skip to content

Commit

Permalink
You can now: set the title of the chest inventory, send a message to …
Browse files Browse the repository at this point in the history
…players at an interval showing their earnings.
  • Loading branch information
biscuut committed Mar 20, 2019
1 parent 0da6fb9 commit 633bb54
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 20 deletions.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>codes.biscuit</groupId>
<artifactId>SellChest</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<packaging>jar</packaging>

<name>SellChest</name>
Expand Down Expand Up @@ -74,6 +74,18 @@
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>org.spigotmc</groupId>-->
<!--<artifactId>paperspigot</artifactId>-->
<!--<version>1.8.8</version>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>VaultAPI</artifactId>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/codes/biscuit/sellchest/SellChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class SellChest extends JavaPlugin {

@Override
public void onEnable() {
getConfig().options().copyDefaults(true);
saveDefaultConfig();
getCommand("sellchest").setExecutor(new SellChestCommand(this));
configValues = new ConfigValues(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
}
sender.sendMessage(Utils.color("&aGave " + p.getName() + " " + giveAmount + " sellchest(s)!"));
if (!main.getConfigValues().getMessageReceive(giveAmount).equals("")) p.sendMessage(main.getConfigValues().getMessageReceive(giveAmount));
main.getUtils().sendMessage(p, ConfigValues.Message.RECEIVED, giveAmount);
} else {
sender.sendMessage(Utils.color("&cThis player doesn't have an empty slot in their inventory!"));
}
Expand All @@ -95,6 +95,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
main.getUtils().runSellTimer();
Bukkit.getScheduler().cancelTask(main.getUtils().getSaveTimerID());
main.getUtils().runSaveTimer();
Bukkit.getScheduler().cancelTask(main.getUtils().getMessageTimerID());
main.getUtils().runMessageTimer();
sender.sendMessage(Utils.color("&aReloaded the config! Most values have been instantly updated."));
break;
case "bypass":
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/codes/biscuit/sellchest/events/PlayerEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import codes.biscuit.sellchest.SellChest;
import codes.biscuit.sellchest.utils.ConfigValues;
import codes.biscuit.sellchest.utils.ReflectionUtils;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.Sound;
Expand All @@ -22,6 +23,10 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Chest;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class PlayerEvents implements Listener {
Expand All @@ -32,6 +37,7 @@ public PlayerEvents(SellChest main) {
this.main = main;
}

@SuppressWarnings("ConstantConditions")
@EventHandler(priority = EventPriority.HIGHEST)
public void onVoidChestPlace(PlayerInteractEvent e) {
Player p = e.getPlayer();
Expand Down Expand Up @@ -76,6 +82,36 @@ public void onVoidChestPlace(PlayerInteractEvent e) {
Chest chest = new Chest(main.getUtils().getOppositeDirection(p));
state.setData(chest);
state.update();
try {
if (ReflectionUtils.getVersion().contains("1_8") || ReflectionUtils.getVersion().contains("1_9") || ReflectionUtils.getVersion().contains("1_10") || ReflectionUtils.getVersion().contains("1_11")) {
Method getTileEntity = ReflectionUtils.getMethod(state.getClass(), "getTileEntity");//newBlock.getState().getClass().getDeclaredMethod("getTileEntity");
if (!getTileEntity.isAccessible()) {
getTileEntity.setAccessible(true);
}
Object tileEntityChest = getTileEntity.invoke(newBlock.getState());
Method setDisplayName = ReflectionUtils.getMethod(tileEntityChest.getClass(), "a", String.class); //tileEntityChest.getClass().getMethod("a", String.class);
setDisplayName.invoke(tileEntityChest, main.getConfigValues().getChestTitle());
} else if (ReflectionUtils.getVersion().contains("1_12")) {
Field tileEntity = ReflectionUtils.getField(state.getClass().getSuperclass().getSuperclass().getSuperclass(), "tileEntity");//newBlock.getState().getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("tileEntity");
tileEntity.setAccessible(true);
Object tileEntityObject = tileEntity.get(newBlock.getState());
Field customName = ReflectionUtils.getField(tileEntityObject.getClass().getSuperclass(), "o");//tileEntityObject.getClass().getSuperclass().getDeclaredField("o");
customName.setAccessible(true);
customName.set(tileEntityObject, main.getConfigValues().getChestTitle());
} else {
Method getTileEntity = ReflectionUtils.getMethod(state.getClass(), "getTileEntity");//newBlock.getState().getClass().getDeclaredMethod("getTileEntity");
if (!getTileEntity.isAccessible()) {
getTileEntity.setAccessible(true);
}
Object tileEntityChest = getTileEntity.invoke(newBlock.getState());
Method setDisplayName = ReflectionUtils.getMethod(tileEntityChest.getClass(), "setCustomName", ReflectionUtils.getNMSClass("IChatBaseComponent")); //tileEntityChest.getClass().getMethod("setCustomName", ReflectionUtils.getNMSClass("IChatBaseComponent"));
Constructor newChatComponentText = ReflectionUtils.getConstructor(ReflectionUtils.getNMSClass("ChatComponentText"), String.class); //ReflectionUtils.getNMSClass("ChatComponentText").getConstructor(String.class);
Object chatComponentText = newChatComponentText.newInstance(main.getConfigValues().getChestTitle());
setDisplayName.invoke(tileEntityChest, chatComponentText);
}
} catch (IllegalAccessException | InvocationTargetException | InstantiationException | NullPointerException ex) {
ex.printStackTrace();
}
Sound digSound = null;
try {
digSound = Sound.valueOf("DIG_WOOD"); // Sound for 1.8
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/codes/biscuit/sellchest/hooks/HookUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class HookUtils {
private SellChest main;
private Economy economy;
private Map<Hooks, Object> enabledHooks = new HashMap<>();
private Map<OfflinePlayer, Double> moneyHistory = new HashMap<>();

public HookUtils(SellChest main) {
this.main = main;
Expand Down Expand Up @@ -67,6 +68,9 @@ public double getValue(ItemStack sellItem, Player p) {
}

public void giveMoney(OfflinePlayer p, double amount, Location loc) {
if (main.getConfigValues().getRecentEarningsInterval() > 0) {
moneyHistory.put(p, moneyHistory.getOrDefault(p, 0D) + amount);
}
MoneyRecipient mr = main.getConfigValues().getMoneyRecipient();
FactionsUUIDHook factionsUUIDHook = ((FactionsUUIDHook)enabledHooks.get(Hooks.FACTIONSUUID));
MassiveCoreHook massiveCoreHook = ((MassiveCoreHook)enabledHooks.get(Hooks.MASSIVECOREFACTIONS));
Expand Down Expand Up @@ -159,6 +163,10 @@ Economy getEconomy() {
return economy;
}

public Map<OfflinePlayer, Double> getMoneyHistory() {
return moneyHistory;
}

enum Hooks {
MASSIVECOREFACTIONS,
FACTIONSUUID, // and all forks like SavageFactions
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/codes/biscuit/sellchest/utils/ConfigValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ public void setupSellChests() {
}
main.getUtils().runSellTimer();
main.getUtils().runSaveTimer();
main.getUtils().runMessageTimer();
}

String getChestName() {
return ChatColor.translateAlternateColorCodes('&', main.getConfig().getString("item.name"));
}

public String getChestTitle() {
return ChatColor.translateAlternateColorCodes('&', main.getConfig().getString("chest-title"));
}

long getSellInterval() {
long ticks = Math.round(main.getConfig().getDouble("sell-time") * 20);
if (ticks >= 1) {
Expand All @@ -82,19 +87,14 @@ long getSaveInterval() {
}
}

boolean removeUnsellableItems() {
return main.getConfig().getBoolean("remove-unsellable-items");
}

public String getMessageReceive(int giveAmount) {
return getMessage(Message.RECEIVED).replace("{amount}", String.valueOf(giveAmount));
public long getRecentEarningsInterval() {
return Math.round(main.getConfig().getDouble("recent-earnings-time") * 20);
}

String getReachedLimitMessage(int limit) {
return getMessage(Message.REACHED_LIMIT).replace("{limit}", String.valueOf(limit));
boolean removeUnsellableItems() {
return main.getConfig().getBoolean("remove-unsellable-items");
}


List<String> getChestLore() {
return main.getUtils().colorLore(main.getConfig().getStringList("item.lore"));
}
Expand Down Expand Up @@ -195,7 +195,14 @@ Map<String, Integer> getChestLimits() {
return limits;
}

String getMessage(Message message) {
String getMessage(Message message, Object... variables) {
if (message == Message.RECEIVED) {
return Utils.color(main.getConfig().getString(message.getPath()).replace("{amount}", String.valueOf(variables[0])));
} else if (message == Message.REACHED_LIMIT) {
return Utils.color(main.getConfig().getString(message.getPath()).replace("{limit}", String.valueOf(variables[0])));
} else if (message == Message.RECENTLY_EARNED) {
return Utils.color(main.getConfig().getString(message.getPath()).replace("{amount}", String.valueOf(variables[0])));
}
return Utils.color(main.getConfig().getString(message.getPath()));
}

Expand All @@ -210,6 +217,7 @@ public enum Message {
NO_PERMISSION_PLACE("no-permission-place"),
NO_PERMISSION_COMMAND("no-permission-command"),
NOT_MINIMUM_FACTION("not-minimum-faction"),
RECENTLY_EARNED("recently-earned"),
REACHED_LIMIT("reached-limit");

private String path;
Expand Down
181 changes: 181 additions & 0 deletions src/main/java/codes/biscuit/sellchest/utils/ReflectionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package codes.biscuit.sellchest.utils;

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

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ReflectionUtils {

/*
* The server version string to location NMS & OBC classes
*/
private static String versionString;

/*
* Cache of NMS classes that we've searched for
*/
private static Map<String, Class<?>> loadedNMSClasses = new HashMap<>();

/*
* Cache of OBS classes that we've searched for
*/
private static Map<String, Class<?>> loadedOBCClasses = new HashMap<>();

/*
* Cache of methods that we've found in particular classes
*/
private static Map<Class<?>, Map<String, Method>> loadedMethods = new HashMap<>();

/*
* Cache of fields that we've found in particular classes
*/
private static Map<Class<?>, Map<String, Field>> loadedFields = new HashMap<>();

/**
* Gets the version string for NMS & OBC class paths
*
* @return The version string of OBC and NMS packages
*/
public static String getVersion() {
if (versionString == null) {
String name = Bukkit.getServer().getClass().getPackage().getName();
versionString = name.substring(name.lastIndexOf('.') + 1) + ".";
}

return versionString;
}

/**
* Get an NMS Class
*
* @param nmsClassName The name of the class
* @return The class
*/
public static Class<?> getNMSClass(String nmsClassName) {
if (loadedNMSClasses.containsKey(nmsClassName)) {
return loadedNMSClasses.get(nmsClassName);
}

String clazzName = "net.minecraft.server." + getVersion() + nmsClassName;
Class<?> clazz;

try {
clazz = Class.forName(clazzName);
} catch (Throwable t) {
t.printStackTrace();
return loadedNMSClasses.put(nmsClassName, null);
}

loadedNMSClasses.put(nmsClassName, clazz);
return clazz;
}

/**
* Get a class from the org.bukkit.craftbukkit package
*
* @param obcClassName the path to the class
* @return the found class at the specified path
*/
public synchronized static Class<?> getOBCClass(String obcClassName) {
if (loadedOBCClasses.containsKey(obcClassName)) {
return loadedOBCClasses.get(obcClassName);
}

String clazzName = "org.bukkit.craftbukkit." + getVersion() + obcClassName;
Class<?> clazz;

try {
clazz = Class.forName(clazzName);
} catch (Throwable t) {
t.printStackTrace();
loadedOBCClasses.put(obcClassName, null);
return null;
}

loadedOBCClasses.put(obcClassName, clazz);
return clazz;
}

/**
* Get a classes constructor
*
* @param clazz The constructor class
* @param params The parameters in the constructor
* @return The constructor object
*/
public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... params) {
try {
return clazz.getDeclaredConstructor(params);
} catch (NoSuchMethodException e) {
return null;
}
}

/**
* Get a method from a class that has the specific paramaters
*
* @param clazz The class we are searching
* @param methodName The name of the method
* @param params Any parameters that the method has
* @return The method with appropriate paramaters
*/
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
if (!loadedMethods.containsKey(clazz)) {
loadedMethods.put(clazz, new HashMap<>());
}

Map<String, Method> methods = loadedMethods.get(clazz);

if (methods.containsKey(methodName)) {
return methods.get(methodName);
}

try {
Method method = clazz.getDeclaredMethod(methodName, params);
methods.put(methodName, method);
loadedMethods.put(clazz, methods);
return method;
} catch (Exception e) {
e.printStackTrace();
methods.put(methodName, null);
loadedMethods.put(clazz, methods);
return null;
}
}

/**
* Get a field with a particular name from a class
*
* @param clazz The class
* @param fieldName The name of the field
* @return The field object
*/
public static Field getField(Class<?> clazz, String fieldName) {
if (!loadedFields.containsKey(clazz)) {
loadedFields.put(clazz, new HashMap<>());
}

Map<String, Field> fields = loadedFields.get(clazz);

if (fields.containsKey(fieldName)) {
return fields.get(fieldName);
}

try {
Field field = clazz.getDeclaredField(fieldName);
fields.put(fieldName, field);
loadedFields.put(clazz, fields);
return field;
} catch (Exception e) {
e.printStackTrace();
fields.put(fieldName, null);
loadedFields.put(clazz, fields);
return null;
}
}
}
Loading

0 comments on commit 633bb54

Please sign in to comment.