add mouse wheel plugin

This commit is contained in:
vfyjxf committed 2021-08-27 22:37:29 +08:00
1 parent dfc5185030
commit 288a8531af
7 files changed
+147 -6

No files matched your search

@@ -1,12 +1,25 @@
package com.github.vfyjxf.nee;
import appeng.client.gui.implementations.GuiPatternTerm;
import appeng.container.slot.SlotFake;
import com.github.vfyjxf.nee.network.NEENetworkHandler;
import com.github.vfyjxf.nee.network.packet.PacketStackCountChange;
import com.github.vfyjxf.nee.utils.GuiUtils;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Slot;
import net.minecraft.launchwrapper.Launch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.input.Mouse;
import java.io.File;
@@ -23,15 +36,40 @@ public class NotEnoughEnergistics {
public static final String DEPENDENCIES = "required-after:NotEnoughItems;required-after:appliedenergistics2";
public static final Logger logger = LogManager.getLogger("NotEnoughEnergistics");
@Mod.EventHandler
@EventHandler
public void preInit(FMLPreInitializationEvent e) {
NEENetworkHandler.init();
NEEConfig.loadConfig(new File(Launch.minecraftHome, "config/NotEnoughEnergistics.cfg"));
}
@Mod.EventHandler
private void serverStarting(FMLServerStartingEvent event) {
@EventHandler
public void init(FMLInitializationEvent event) {
if (FMLCommonHandler.instance().getSide().isClient()) {
FMLCommonHandler.instance().bus().register(this);
}
}
@EventHandler
public void serverStarting(FMLServerStartingEvent event) {
event.registerServerCommand(new NEECommands());
}
@SubscribeEvent
public void onRenderTick(TickEvent.RenderTickEvent event) {
if (event.phase == TickEvent.Phase.START) {
Minecraft mc = Minecraft.getMinecraft();
int i = Mouse.getDWheel();
if (i != 0 && mc.currentScreen instanceof GuiPatternTerm) {
GuiPatternTerm guiPatternTerm = (GuiPatternTerm) mc.currentScreen;
int x = Mouse.getEventX() * guiPatternTerm.width / mc.displayWidth;
int y = guiPatternTerm.height - Mouse.getEventY() * guiPatternTerm.height / mc.displayHeight - 1;
Slot currentSlot = GuiUtils.getSlotUnderMouse(guiPatternTerm, x, y);
if (currentSlot instanceof SlotFake && currentSlot.getHasStack()) {
int changeCount = GuiContainer.isCtrlKeyDown() ? i / 60 : i / 120;
NEENetworkHandler.getInstance().sendToServer(new PacketStackCountChange(currentSlot.slotNumber, changeCount));
}
}
}
}
}
@@ -4,6 +4,7 @@ import com.github.vfyjxf.nee.NotEnoughEnergistics;
import com.github.vfyjxf.nee.network.packet.PacketArcaneRecipe;
import com.github.vfyjxf.nee.network.packet.PacketExtremeRecipe;
import com.github.vfyjxf.nee.network.packet.PacketNEIPatternRecipe;
import com.github.vfyjxf.nee.network.packet.PacketStackCountChange;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
@@ -19,6 +20,7 @@ public class NEENetworkHandler {
INSTANCE.registerMessage(PacketNEIPatternRecipe.class, PacketNEIPatternRecipe.class, 0, Side.SERVER);
INSTANCE.registerMessage(PacketArcaneRecipe.class, PacketArcaneRecipe.class, 1, Side.SERVER);
INSTANCE.registerMessage(PacketExtremeRecipe.class, PacketExtremeRecipe.class, 2, Side.SERVER);
INSTANCE.registerMessage(PacketStackCountChange.class, PacketStackCountChange.class, 3, Side.SERVER);
}
}
@@ -0,0 +1,72 @@
package com.github.vfyjxf.nee.network.packet;
import appeng.container.implementations.ContainerPatternTerm;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
/**
* @author vfyjxf
*/
public class PacketStackCountChange implements IMessage, IMessageHandler<PacketStackCountChange, IMessage> {
private int slotIndex;
private int changeCount;
public int getSlotIndex() {
return slotIndex;
}
public int getChangeCount() {
return changeCount;
}
public PacketStackCountChange() {
}
public PacketStackCountChange(int slotIndex, int changeCount) {
this.slotIndex = slotIndex;
this.changeCount = changeCount;
}
@Override
public void fromBytes(ByteBuf buf) {
this.slotIndex = buf.readInt();
this.changeCount = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.slotIndex);
buf.writeInt(this.changeCount);
}
@Override
public IMessage onMessage(PacketStackCountChange message, MessageContext ctx) {
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
Container container = player.openContainer;
if (container instanceof ContainerPatternTerm && !((ContainerPatternTerm) container).isCraftingMode()) {
Slot currentSlot = container.getSlot(message.getSlotIndex());
for (int i = 0; i < Math.abs(message.getChangeCount()); i++) {
if (message.getChangeCount() > 0) {
if (currentSlot.getStack().stackSize == currentSlot.getStack().getMaxStackSize()) {
break;
}
currentSlot.getStack().stackSize = (currentSlot.getStack().stackSize + 1);
} else {
if (currentSlot.getStack().stackSize - 1 == 0) {
break;
}
currentSlot.getStack().stackSize = currentSlot.getStack().stackSize - 1;
}
}
}
return null;
}
}
@@ -0,0 +1,27 @@
package com.github.vfyjxf.nee.utils;
import appeng.client.gui.AEBaseGui;
import cpw.mods.fml.relauncher.ReflectionHelper;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Slot;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author vfyjxf
*/
public class GuiUtils {
public static Slot getSlotUnderMouse(GuiContainer guiContainer, int mouseX, int mouseY) {
Method getSlotMethod = ReflectionHelper.findMethod(AEBaseGui.class, (AEBaseGui) guiContainer, new String[]{"getSlot"}, int.class, int.class);
try {
return (Slot) getSlotMethod.invoke(guiContainer,mouseX,mouseY);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
}