Skip to content

Commit

Permalink
Allow Cycling P2Ps via Shift+Scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jul 20, 2024
1 parent 6a4ff3b commit 6d71576
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 4 deletions.
13 changes: 10 additions & 3 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ dependencies {
compileOnly rfg.deobf("curse.maven:chisel-235279:2915375") // Version 1.0.2.45

// AE2, Used in Naquadah Reactors (AE2 Unofficial Extended Life used) (from CurseForge)
// This Mod has a Temporary Mixin to fix a Bug, which may be fixed in a Later Version!
// Check/Remove the mixin after updating!
compileOnly rfg.deobf("curse.maven:ae2-extended-life-570458:5378163") // Version 0.56.5

// DeepMobEvolution, Used in DME Data Hatch (from CurseForge)
// NAE2, Used in P2P Shift Scrolling (from CurseForge)
compileOnly rfg.deobf("curse.maven:nae2-884359:5380800") // Version 1.6.4

// DeepMobEvolution, Used in DME Sim Chamber (from CurseForge)
compileOnly rfg.deobf("curse.maven:dme-737252:5043404") // Version 1.2.2

// Extended Crafting, Used in Naquadah Reactors (Nomifactory Fork Used) (from CurseForge)
Expand Down Expand Up @@ -174,12 +179,14 @@ dependencies {
runtimeOnly "curse.maven:ctm-267602:2915363" // Version 1.0.2.31
}

// This Mod has a Temporary Mixin to fix a Bug, which may be fixed in a Later Version!
// Check/Remove the mixin after updating!
if (project.enable_ae2.toBoolean()) {
runtimeOnly "curse.maven:ae2-extended-life-570458:5378163" // Version 0.56.5
}

if (project.enable_nae2.toBoolean()) {
runtimeOnly "curse.maven:nae2-884359:5380800" // Version 1.6.4
}

if (project.enable_dme.toBoolean()) {
// Dep: Patchouli
runtimeOnly "curse.maven:patchouli-306770:3162874" // Version 1.0-23.6
Expand Down
5 changes: 4 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ enable_chisel = false
# If this is set to false, those blocks will be set to air. The mixin will not apply.
enable_ae2 = false

# Whether to enable DME in runtime. Enables the DME Dat Hatch.
# Whether to enable NAE2 in runtime. Requires enable_ae2 to be true.
enable_nae2 = false

# Whether to enable DME in runtime. Enables the DME Sim Chamber.
enable_dme = false

# Whether to enable Extended Crafting in runtime. Enables Extended Crafting Blocks in DME Sim Chamber and Naq Reactors.
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/nomiceu/nomilabs/LabsValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class LabsValues {
public static final String ENDER_STORAGE_MODID = "enderstorage";
public static final String EXTENDED_CRAFTING_MODID = "extendedcrafting";
public static final String AE2_MODID = "appliedenergistics2";
public static final String NAE2_MODID = "nae2";
public static final String CHISEL_MODID = "chisel";
public static final String DRACONIC_MODID = "draconicevolution";
public static final String NUCLEARCRAFT_MODID = "nuclearcraft";
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/event/ClientProxy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.nomiceu.nomilabs.event;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.EnumHand;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.fml.client.FMLClientHandler;
Expand All @@ -19,8 +23,11 @@
import com.nomiceu.nomilabs.gregtech.block.registry.LabsMetaBlocks;
import com.nomiceu.nomilabs.integration.betterquesting.LabsTierHelper;
import com.nomiceu.nomilabs.item.registry.LabsItems;
import com.nomiceu.nomilabs.network.LabsNetworkHandler;
import com.nomiceu.nomilabs.network.LabsP2PCycleMessage;
import com.nomiceu.nomilabs.tooltip.LabsTooltipHelper;
import com.nomiceu.nomilabs.tooltip.TooltipAdder;
import com.nomiceu.nomilabs.util.ItemMeta;

/*
* Every texture is registered, in case something in that registry, not in that config, is enabled.
Expand Down Expand Up @@ -83,4 +90,30 @@ public static void afterScriptLoad(ScriptRunEvent.Post event) {
NomiLabs.LOGGER.info("Reloading Options File.");
FMLClientHandler.instance().getClient().gameSettings.loadOptions();
}

@SubscribeEvent
public static void onMouseEvent(MouseEvent event) {
if (!Loader.isModLoaded(LabsValues.AE2_MODID)) return;

int scroll = event.getDwheel();
if (scroll == 0 || !LabsTooltipHelper.isShiftDown()) return;
byte offset = (byte) (scroll < 0 ? -1 : 1);

// Handle P2P Scroll
Minecraft minecraft = Minecraft.getMinecraft();
EntityPlayer player = minecraft.player;
if (player == null || minecraft.currentScreen != null) return;

if (LabsP2PCycleMessage.MessageHandler.getP2ps()
.containsKey(new ItemMeta(player.getHeldItem(EnumHand.MAIN_HAND)))) {
LabsNetworkHandler.NETWORK_HANDLER
.sendToServer(new LabsP2PCycleMessage(player, EnumHand.MAIN_HAND, offset));
event.setCanceled(true);
} else if (LabsP2PCycleMessage.MessageHandler.getP2ps()
.containsKey(new ItemMeta(player.getHeldItem(EnumHand.OFF_HAND)))) {
LabsNetworkHandler.NETWORK_HANDLER
.sendToServer(new LabsP2PCycleMessage(player, EnumHand.OFF_HAND, offset));
event.setCanceled(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nomiceu.nomilabs.network;

import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
Expand All @@ -21,6 +22,8 @@ public static void preInit() {
CURRENT_ID = 0;
registerMessage(CanEditChunkDataMessage.MessageHandler.class, CanEditChunkDataMessage.class);
registerMessage(LabsDifficultyChangeMessage.MessageHandler.class, LabsDifficultyChangeMessage.class);
if (Loader.isModLoaded(LabsValues.AE2_MODID))
registerMessage(LabsP2PCycleMessage.MessageHandler.class, LabsP2PCycleMessage.class);
}

@SuppressWarnings("SameParameterValue")
Expand Down
133 changes: 133 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/network/LabsP2PCycleMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.nomiceu.nomilabs.network;

import java.util.Objects;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Optional;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.nomiceu.nomilabs.LabsValues;
import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.util.ItemMeta;

import appeng.api.AEApi;
import appeng.api.definitions.IItemDefinition;
import co.neeve.nae2.NAE2;
import io.netty.buffer.ByteBuf;

public class LabsP2PCycleMessage implements IMessage {

private int hand;
private byte offset;
private int expectedSlot;

@SuppressWarnings("DataFlowIssue")
public LabsP2PCycleMessage() {
hand = EnumHand.MAIN_HAND.ordinal();
offset = 0;
expectedSlot = 0;
}

public LabsP2PCycleMessage(EntityPlayer player, EnumHand hand, byte offset) {
this.hand = hand.ordinal();
this.offset = offset;
this.expectedSlot = hand == EnumHand.MAIN_HAND ? player.inventory.currentItem : 0; // If offhand, doesn't matter
}

public int getOffset() {
return offset;
}

public EnumHand getHand() {
return EnumHand.values()[hand];
}

public boolean checkValid(EntityPlayer player) {
return getHand() == EnumHand.OFF_HAND || player.inventory.currentItem == expectedSlot;
}

@Override
public void fromBytes(ByteBuf buf) {
hand = buf.readInt();
offset = buf.readByte();
expectedSlot = buf.readInt();
}

@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(hand);
buf.writeByte(offset);
buf.writeInt(expectedSlot);
}

public static class MessageHandler implements IMessageHandler<LabsP2PCycleMessage, IMessage> {

private static BiMap<ItemMeta, Integer> p2ps = null;

public static BiMap<ItemMeta, Integer> getP2ps() {
if (p2ps != null) return p2ps;

p2ps = HashBiMap.create();

// Add AE2 P2Ps
var parts = AEApi.instance().definitions().parts();
addP2pFromDef(parts.p2PTunnelME());
addP2pFromDef(parts.p2PTunnelRedstone());
addP2pFromDef(parts.p2PTunnelItems());
addP2pFromDef(parts.p2PTunnelFluids());
addP2pFromDef(parts.p2PTunnelLight());
addP2pFromDef(parts.p2PTunnelFE());
addP2pFromDef(parts.p2PTunnelEU());
addP2pFromDef(parts.p2PTunnelGTEU());

// Add NAE2 P2Ps
if (Loader.isModLoaded(LabsValues.NAE2_MODID))
addNae2P2ps();

return p2ps;
}

@Optional.Method(modid = LabsValues.NAE2_MODID)
private static void addNae2P2ps() {
addP2pFromDef(NAE2.definitions().parts().p2pTunnelInterface());
}

private static void addP2pFromDef(IItemDefinition def) {
def.maybeStack(1).ifPresent(itemStack -> p2ps.put(new ItemMeta(itemStack), p2ps.size()));
}

private static void tryCycle(EntityPlayer player, LabsP2PCycleMessage cycle) {
if (cycle.checkValid(player)) {
ItemStack heldStack = player.getHeldItem(cycle.getHand());
ItemMeta held = new ItemMeta(heldStack);
if (!getP2ps().containsKey(held)) {
var profile = player.getGameProfile();
String item = Objects.toString(held.getItem().getRegistryName(), "an unregistered item");
NomiLabs.LOGGER.warn("{} ({}) tried to cycle {}", profile.getName(), profile.getId(), item);
return;
}
ItemStack next = getP2ps().inverse().get((getP2ps().get(held) + 1) % p2ps.size()).toStack();
next.setCount(heldStack.getCount());
player.setHeldItem(cycle.getHand(), next);
}
}

@Override
public IMessage onMessage(LabsP2PCycleMessage message, MessageContext ctx) {
if (ctx.side == Side.CLIENT) return null;

FMLCommonHandler.instance().getMinecraftServerInstance()
.addScheduledTask(() -> tryCycle(ctx.getServerHandler().player, message));
return null;
}
}
}

0 comments on commit 6d71576

Please sign in to comment.