Skip to content

Instantly share code, notes, and snippets.

@dashaw92
Created March 9, 2019 16:18
Show Gist options
  • Save dashaw92/f339e4baeeb20ac1e43365b63b77265a to your computer and use it in GitHub Desktop.
Save dashaw92/f339e4baeeb20ac1e43365b63b77265a to your computer and use it in GitHub Desktop.
Modification to EssentialsX Commandsell.java to provide hooking into when people sell items to the server
diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java
index e6c41a98..8897b6b5 100644
--- a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java
+++ b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java
@@ -11,6 +11,9 @@ import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.function.BiConsumer;
import java.util.logging.Level;
import static com.earth2me.essentials.I18n.tl;
@@ -21,6 +24,36 @@ public class Commandsell extends EssentialsCommand {
super("sell");
}
+ //DANNY PATCH BEGIN
+ private static int callback_id = 0;
+ private static Map<Integer, BiConsumer<ItemStack, BigDecimal>> sell_callbacks = new HashMap<>();
+
+ /**
+ * Adds a callback
+ * @return The id of the callback. Used to remove the callback.
+ **/
+ public static int onSell(BiConsumer<ItemStack, BigDecimal> callback) {
+ sell_callbacks.put(callback_id, callback);
+ return callback_id++;
+ }
+
+ /**
+ * Removes a callback
+ **/
+ public static void endCallback(int id) {
+ if(sell_callbacks.containsKey(id)) {
+ sell_callbacks.remove(id);
+ }
+ }
+
+ private void doCallbacks(ItemStack item, BigDecimal money) {
+ sell_callbacks.values()
+ .stream()
+ .filter(n -> n != null)
+ .forEach(cb -> cb.accept(item, money));
+ }
+ //DANNY PATCH END
+
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
BigDecimal totalWorth = BigDecimal.ZERO;
@@ -91,6 +124,11 @@ public class Commandsell extends EssentialsCommand {
// This should never happen.
throw new IllegalStateException("Trying to remove more items than are available.");
}
+
+ //DANNY PATCH BEGIN
+ doCallbacks(ris, result);
+ //DANNY PATCH END
+
user.getBase().getInventory().removeItem(ris);
user.getBase().updateInventory();
Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), ess);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment