Browse Source

container click by core mod

Kajetan Johannes Hammerle 2 years ago
parent
commit
502240dc66

+ 1 - 0
src/main/java/me/km/Server.java

@@ -132,6 +132,7 @@ public class Server {
         scriptEvents = new ScriptEvents(scripts, server);
         Hooks.setBlockHarvest(scriptEvents);
         Hooks.setCraft(scriptEvents);
+        Hooks.setContainerClick(scriptEvents);
         MinecraftForge.EVENT_BUS.register(scriptEvents);
 
         // command manager

+ 0 - 161
src/main/java/me/km/snuviscript/ModContainer.java

@@ -1,161 +0,0 @@
-package me.km.snuviscript;
-
-import me.km.Server;
-import me.km.utils.ReflectionUtils;
-import net.minecraft.entity.player.PlayerEntity;
-import net.minecraft.inventory.IInventory;
-import net.minecraft.inventory.container.ClickType;
-import net.minecraft.inventory.container.Container;
-import net.minecraft.inventory.container.ContainerType;
-import net.minecraft.inventory.container.IContainerListener;
-import net.minecraft.inventory.container.Slot;
-import net.minecraft.item.ItemStack;
-import net.minecraft.util.IIntArray;
-import net.minecraft.util.IntReferenceHolder;
-import net.minecraft.util.NonNullList;
-import net.minecraft.world.World;
-import net.minecraft.entity.player.ServerPlayerEntity;
-
-public class ModContainer extends Container {
-    private final Container container;
-
-    private static ContainerType<?> getType(Container c) {
-        try {
-            return c.getType();
-        } catch(Exception ex) {
-            return null;
-        }
-    }
-
-    public ModContainer(Container c) {
-        super(getType(c), c.windowId);
-        this.container = c;
-    }
-
-    public String getName() {
-        return container.getClass().getSimpleName();
-    }
-
-    @Override
-    public ItemStack slotClick(int slot, int dragType, ClickType ct, PlayerEntity p) {
-        if(Server.scriptEvents.onContainerClick(this, slot, dragType, ct, p)) {
-            Server.scheduler.scheduleTask(() -> {
-                ((ServerPlayerEntity) p).sendAllContents(this, getInventory());
-            });
-            return ItemStack.EMPTY;
-        }
-        return container.slotClick(slot, dragType, ct, p);
-    }
-
-    @Override
-    public boolean canInteractWith(PlayerEntity p) {
-        return container.canInteractWith(p);
-    }
-
-    @Override
-    public ContainerType<?> getType() {
-        return container.getType();
-    }
-
-    @Override
-    protected Slot addSlot(Slot slotIn) {
-        return ReflectionUtils.addSlot(container, slotIn);
-    }
-
-    @Override
-    protected IntReferenceHolder trackInt(IntReferenceHolder intIn) {
-        return ReflectionUtils.trackInt(container, intIn);
-    }
-
-    @Override
-    protected void trackIntArray(IIntArray arrayIn) {
-        ReflectionUtils.trackIntArray(container, arrayIn);
-    }
-
-    @Override
-    public void addListener(IContainerListener listener) {
-        container.addListener(listener);
-    }
-
-    @Override
-    public NonNullList<ItemStack> getInventory() {
-        return container.getInventory();
-    }
-
-    @Override
-    public void detectAndSendChanges() {
-        container.detectAndSendChanges();
-    }
-
-    @Override
-    public boolean enchantItem(PlayerEntity playerIn, int id) {
-        return container.enchantItem(playerIn, id);
-    }
-
-    @Override
-    public Slot getSlot(int slotId) {
-        return container.getSlot(slotId);
-    }
-
-    @Override
-    public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
-        return container.transferStackInSlot(playerIn, index);
-    }
-
-    @Override
-    public boolean canMergeSlot(ItemStack stack, Slot slotIn) {
-        return container.canMergeSlot(stack, slotIn);
-    }
-
-    @Override
-    public void onContainerClosed(PlayerEntity playerIn) {
-        container.onContainerClosed(playerIn);
-    }
-
-    @Override
-    protected void clearContainer(PlayerEntity playerIn, World worldIn, IInventory inventoryIn) {
-        ReflectionUtils.clearContainer(container, playerIn, worldIn, inventoryIn);
-    }
-
-    @Override
-    public void onCraftMatrixChanged(IInventory inventoryIn) {
-        container.onCraftMatrixChanged(inventoryIn);
-    }
-
-    @Override
-    public void putStackInSlot(int slotID, ItemStack stack) {
-        container.putStackInSlot(slotID, stack);
-    }
-
-    @Override
-    public void updateProgressBar(int id, int data) {
-        container.updateProgressBar(id, data);
-    }
-
-    @Override
-    public boolean getCanCraft(PlayerEntity player) {
-        return container.getCanCraft(player);
-    }
-
-    @Override
-    public void setCanCraft(PlayerEntity player, boolean canCraft) {
-        container.setCanCraft(player, canCraft);
-    }
-
-    @Override
-    protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex,
-            boolean reverseDirection) {
-        return ReflectionUtils.mergeItemStack(container, stack, startIndex, endIndex,
-                reverseDirection);
-    }
-
-    @Override
-    protected void resetDrag() {
-        ReflectionUtils.resetDrag(container);
-    }
-
-    @Override
-    public boolean canDragIntoSlot(Slot slotIn) {
-        return container.canDragIntoSlot(slotIn);
-    }
-}

+ 6 - 21
src/main/java/me/km/snuviscript/ScriptEvents.java

@@ -7,6 +7,7 @@ import me.hammerle.snuviscript.code.*;
 import me.hammerle.snuviscript.inputprovider.Variable;
 import me.kcm.BlockHarvest;
 import me.kcm.Craft;
+import me.kcm.ContainerClick;
 import me.km.utils.Utils;
 import me.km.entities.*;
 import me.km.events.CommandEvent;
@@ -18,6 +19,7 @@ import net.minecraft.entity.*;
 import net.minecraft.entity.player.*;
 import net.minecraft.inventory.*;
 import net.minecraft.inventory.container.ClickType;
+import net.minecraft.inventory.container.Container;
 import net.minecraft.item.ItemStack;
 import net.minecraft.item.crafting.ICraftingRecipe;
 import net.minecraft.item.crafting.IRecipeType;
@@ -33,7 +35,6 @@ import net.minecraft.util.text.*;
 import net.minecraft.world.*;
 import net.minecraft.world.server.ServerWorld;
 import net.minecraftforge.event.*;
-import net.minecraftforge.event.TickEvent.PlayerTickEvent;
 import net.minecraftforge.event.entity.*;
 import net.minecraftforge.event.entity.item.ItemTossEvent;
 import net.minecraftforge.event.entity.living.*;
@@ -44,7 +45,7 @@ import net.minecraftforge.eventbus.api.*;
 import net.minecraftforge.event.entity.player.PlayerEvent;
 import net.minecraftforge.eventbus.api.Event.Result;
 
-public class ScriptEvents implements BlockHarvest, Craft {
+public class ScriptEvents implements BlockHarvest, Craft, ContainerClick {
     private static class WrappedBool {
         public boolean wrapped;
     }
@@ -808,25 +809,9 @@ public class ScriptEvents implements BlockHarvest, Craft {
         });
     }
 
-    @SubscribeEvent
-    public void onContainerOpen(PlayerContainerEvent.Open e) {
-        PlayerEntity p = e.getPlayer();
-        if(!(p.openContainer instanceof ModContainer)) {
-            p.openContainer = new ModContainer(p.openContainer);
-        }
-    }
-
-    @SubscribeEvent
-    public void onPlayerTick(PlayerTickEvent e) {
-        PlayerEntity p = e.player;
-        if(!(p.openContainer instanceof ModContainer)) {
-            p.openContainer = new ModContainer(p.openContainer);
-        }
-    }
-
-    public boolean onContainerClick(ModContainer c, int slot, int dragType, ClickType ct,
-            PlayerEntity p) {
-        String name = c.getName();
+    @Override
+    public boolean onClick(Container c, int slot, int dragType, ClickType ct, PlayerEntity p) {
+        String name = c.getClass().getSimpleName();
         WrappedBool b = new WrappedBool();
         b.wrapped = false;
         scripts.getScriptManager().callEvent("container_click", sc -> {

+ 16 - 5
src/main/java/me/km/snuviscript/commands/Commands.java

@@ -3,6 +3,7 @@ package me.km.snuviscript.commands;
 import me.hammerle.snuviscript.code.ScriptManager;
 import me.hammerle.snuviscript.code.SnuviUtils;
 import me.hammerle.snuviscript.config.SnuviConfig;
+import me.hammerle.snuviscript.exceptions.StackTrace;
 import me.km.permissions.ModCommandManager;
 import me.km.permissions.Permissions;
 import me.km.scheduler.SnuviScheduler;
@@ -15,18 +16,28 @@ import net.minecraft.server.MinecraftServer;
 import net.minecraft.util.text.StringTextComponent;
 
 public class Commands {
-    public static void registerFunctions(ScriptManager sm, Scripts scripts, Permissions perms, SnuviScheduler scheduler,
-            MinecraftServer server, ModCommandManager commands) {
+    public static void registerFunctions(ScriptManager sm, Scripts scripts, Permissions perms,
+            SnuviScheduler scheduler, MinecraftServer server, ModCommandManager commands) {
         sm.registerConsumer("setmotd", (sc, in) -> {
-            server.getServerStatusResponse().setServerDescription(new StringTextComponent(in[0].getString(sc)));
+            server.getServerStatusResponse()
+                    .setServerDescription(new StringTextComponent(in[0].getString(sc)));
         });
         sm.registerConsumer("msg", (sc, in) -> {
             sendMessageToGroup(server, scripts, perms, in[0].get(sc), sc, concat(sc, 1, "", in));
         });
-        sm.registerFunction("removeformat", (sc, in) -> SnuviUtils.connect(sc, in, 0).replaceAll("§.", ""));
+        sm.registerFunction("removeformat",
+                (sc, in) -> SnuviUtils.connect(sc, in, 0).replaceAll("§.", ""));
         sm.registerConsumer("command", (sc, in) -> {
             final String s = SnuviUtils.connect(sc, in, 0);
-            scheduler.scheduleTask(() -> commands.handleCommand(server.getCommandSource(), s));
+            StackTrace trace = sc.getStackTrace();
+            scheduler.scheduleTask(() -> {
+                try {
+                    commands.handleCommand(server.getCommandSource(), s);
+                } catch(Exception ex) {
+                    sc.getScriptManager().getLogger().print(null, ex, "living.heal", sc.getName(),
+                            sc, trace);
+                }
+            });
         });
         sm.registerFunction("isplayer", (sc, in) -> {
             Object o = in[0].get(sc);

+ 19 - 2
src/main/java/me/km/snuviscript/commands/LivingCommands.java

@@ -2,6 +2,7 @@ package me.km.snuviscript.commands;
 
 import java.util.UUID;
 import me.hammerle.snuviscript.code.ScriptManager;
+import me.hammerle.snuviscript.exceptions.StackTrace;
 import me.km.scheduler.SnuviScheduler;
 import me.km.utils.Location;
 import me.km.utils.ReflectionUtils;
@@ -97,12 +98,28 @@ public class LivingCommands {
             float damage = in[1].getFloat(sc);
             DamageSource damageSource =
                     (in.length >= 3) ? (DamageSource) in[2].get(sc) : DamageSource.GENERIC;
-            scheduler.scheduleTask(() -> liv.attackEntityFrom(damageSource, damage));
+            StackTrace trace = sc.getStackTrace();
+            scheduler.scheduleTask(() -> {
+                try {
+                    liv.attackEntityFrom(damageSource, damage);
+                } catch(Exception ex) {
+                    sc.getScriptManager().getLogger().print(null, ex, "living.damage", sc.getName(),
+                            sc, trace);
+                }
+            });
         });
         sm.registerConsumer("living.heal", (sc, in) -> {
             LivingEntity liv = (LivingEntity) in[0].get(sc);
             float heal = in[1].getFloat(sc);
-            scheduler.scheduleTask(() -> liv.heal(heal));
+            StackTrace trace = sc.getStackTrace();
+            scheduler.scheduleTask(() -> {
+                try {
+                    liv.heal(heal);
+                } catch(Exception ex) {
+                    sc.getScriptManager().getLogger().print(null, ex, "living.heal", sc.getName(),
+                            sc, trace);
+                }
+            });
         });
         sm.registerFunction("living.shootprojectile",
                 (sc, in) -> launchProjectile((LivingEntity) in[0].get(sc),