Skip to content

Commit

Permalink
Add new tooltip to terminal entries to indicate transmutability from EMC
Browse files Browse the repository at this point in the history
trust me bro it's all worth it
  • Loading branch information
62832 committed May 2, 2024
1 parent 38b2f9d commit 6426cad
Show file tree
Hide file tree
Showing 15 changed files with 528 additions and 8 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ minecraft {
mixin {
add(sourceSets.main.get(), "$modId.refmap.json")
config("$modId.mixins.json")
config("deargodwhy.mixins.json")
}

dependencies {
minecraft(libs.forge)
annotationProcessor(variantOf(libs.mixin) { classifier("processor") })

implementation(fg.deobf(libs.ae2.get()))
implementation(fg.deobf(libs.projecte.get()))

Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencyResolutionManagement {
plugin("spotless", "com.diffplug.spotless").version("6.23.3")

library("forge", "net.neoforged", "forge").version("1.20.1-47.1.54")
library("mixin", "org.spongepowered", "mixin").version("0.8.5")

version("ae2", "15.1.0")
library("ae2", "appeng", "appliedenergistics2-forge").versionRef("ae2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

import appeng.api.features.P2PTunnelAttunement;

@Mixin(P2PTunnelAttunement.class)
@Mixin(value = P2PTunnelAttunement.class, remap = false)
public interface P2PTunnelAttunementAccessor {
@Accessor(value = "tagTunnels", remap = false)
@Accessor
static Map<TagKey<Item>, Item> getTagTunnels() {
throw new AssertionError();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gripe._90.appliede.mixin.retardedfuckingtooltip;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

import net.minecraft.network.FriendlyByteBuf;

import appeng.core.sync.BasePacket;

@Mixin(value = BasePacket.class, remap = false)
public interface BasePacketAccessor {
@Invoker
void invokeConfigureWrite(FriendlyByteBuf data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gripe._90.appliede.mixin.retardedfuckingtooltip;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

import appeng.menu.me.common.GridInventoryEntry;

import gripe._90.appliede.reporting.GridInventoryEMCEntry;

@Mixin(GridInventoryEntry.class)
public abstract class GridInventoryEntryMixin implements GridInventoryEMCEntry {
@Unique
private boolean appliede$transmutable = false;

@Unique
@Override
public boolean appliede$isTransmutable() {
return appliede$transmutable;
}

@Unique
@Override
public void appliede$setTransmutable(boolean extractable) {
appliede$transmutable = extractable;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gripe._90.appliede.mixin.retardedfuckingtooltip;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

import appeng.core.sync.packets.MEInventoryUpdatePacket;

@Mixin(MEInventoryUpdatePacket.class)
public interface MEInventoryUpdatePacketAccessor {
@Invoker(value = "<init>", remap = false)
static MEInventoryUpdatePacket create() {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gripe._90.appliede.mixin.retardedfuckingtooltip;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.network.FriendlyByteBuf;

import appeng.core.sync.packets.MEInventoryUpdatePacket;
import appeng.menu.me.common.GridInventoryEntry;

import gripe._90.appliede.reporting.GridInventoryEMCEntry;

@Mixin(value = MEInventoryUpdatePacket.class, remap = false)
public abstract class MEInventoryUpdatePacketMixin {
// spotless:off
@Redirect(
method = "<init>(Lnet/minecraft/network/FriendlyByteBuf;)V",
at = @At(
value = "INVOKE",
target = "Lappeng/core/sync/packets/MEInventoryUpdatePacket;readEntry(Lnet/minecraft/network/FriendlyByteBuf;)Lappeng/menu/me/common/GridInventoryEntry;"))
// spotless:on
private GridInventoryEntry readEntryWithEmc(FriendlyByteBuf buffer) {
return GridInventoryEMCEntry.readEntry(buffer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package gripe._90.appliede.mixin.retardedfuckingtooltip;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.MenuType;

import appeng.api.networking.IGridNode;
import appeng.api.networking.security.IActionHost;
import appeng.api.stacks.AEItemKey;
import appeng.api.stacks.AEKey;
import appeng.api.stacks.KeyCounter;
import appeng.api.storage.ITerminalHost;
import appeng.menu.AEBaseMenu;
import appeng.menu.me.common.IncrementalUpdateHelper;
import appeng.menu.me.common.MEStorageMenu;

import gripe._90.appliede.reporting.MEInventoryUpdatePacketBuilder;
import gripe._90.appliede.service.KnowledgeService;

@Mixin(value = MEStorageMenu.class, remap = false)
public abstract class MEStorageMenuMixin extends AEBaseMenu {
@Nullable
@Shadow
private IGridNode networkNode;

@Shadow
@Final
private ITerminalHost host;

@Shadow
private Set<AEKey> previousCraftables;

@Shadow
private KeyCounter previousAvailableStacks;

@Shadow
@Final
private IncrementalUpdateHelper updateHelper;

@Unique
private Set<AEItemKey> appliede$previousTransmutables = new HashSet<>();

public MEStorageMenuMixin(MenuType<?> menuType, int id, Inventory playerInventory, ITerminalHost host) {
super(menuType, id, playerInventory, host);
}

@Shadow
protected abstract boolean showsCraftables();

@Shadow
public abstract boolean isKeyVisible(AEKey key);

@Shadow
protected abstract void updatePowerStatus();

@Unique
private Set<AEItemKey> appliede$getTransmutablesFromGrid() {
var hostNode = networkNode;

if (hostNode == null && host instanceof IActionHost actionHost) {
hostNode = actionHost.getActionableNode();
}

if (!showsCraftables()) {
return Collections.emptySet();
}

return hostNode != null && hostNode.isActive()
? hostNode.getGrid().getService(KnowledgeService.class).getKnownItems()
: Collections.emptySet();
}

// spotless:off
@SuppressWarnings("Convert2MethodRef")
@Inject(
method = "broadcastChanges",
at = @At(
value = "INVOKE",
target = "Lappeng/menu/me/common/IncrementalUpdateHelper;hasChanges()Z",
remap = false),
cancellable = true,
locals = LocalCapture.CAPTURE_FAILEXCEPTION,
remap = true)
// spotless:on
private void replacePacket(CallbackInfo ci, Set<AEKey> craftable, KeyCounter availableStacks) {
ci.cancel();

var transmutable = appliede$getTransmutablesFromGrid();
Sets.difference(appliede$previousTransmutables, transmutable).forEach(updateHelper::addChange);
Sets.difference(transmutable, appliede$previousTransmutables).forEach(updateHelper::addChange);

if (updateHelper.hasChanges()) {
var builder = new MEInventoryUpdatePacketBuilder(containerId, updateHelper.isFullUpdate());
builder.setFilter(key -> isKeyVisible(key));
builder.addChanges(updateHelper, availableStacks, craftable, new KeyCounter(), transmutable);
builder.buildAndSend(packet -> sendPacketToClient(packet));
updateHelper.commitChanges();
}

previousCraftables = ImmutableSet.copyOf(craftable);
previousAvailableStacks = availableStacks;
appliede$previousTransmutables = ImmutableSet.copyOf(transmutable);

updatePowerStatus();
super.broadcastChanges();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gripe._90.appliede.mixin.retardedfuckingtooltip;

import java.util.List;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;

import appeng.client.gui.AEBaseScreen;
import appeng.client.gui.me.common.MEStorageScreen;
import appeng.client.gui.style.ScreenStyle;
import appeng.menu.me.common.GridInventoryEntry;
import appeng.menu.me.common.MEStorageMenu;

import gripe._90.appliede.AppliedE;
import gripe._90.appliede.reporting.GridInventoryEMCEntry;

@Mixin(MEStorageScreen.class)
public abstract class MEStorageScreenMixin<C extends MEStorageMenu> extends AEBaseScreen<C> {
public MEStorageScreenMixin(C menu, Inventory playerInventory, Component title, ScreenStyle style) {
super(menu, playerInventory, title, style);
}

// spotless:off
@Inject(
method = "renderGridInventoryEntryTooltip",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/Minecraft;getInstance()Lnet/minecraft/client/Minecraft;",
remap = true),
locals = LocalCapture.CAPTURE_FAILEXCEPTION,
remap = false)
// spotless:on
private void addTransmutable(
GuiGraphics guiGraphics,
GridInventoryEntry entry,
int x,
int y,
CallbackInfo ci,
List<Component> currentTooltip) {
if (((GridInventoryEMCEntry) entry).appliede$isTransmutable() && entry.getStoredAmount() == 0) {
currentTooltip.add(Component.translatable("tooltip." + AppliedE.MODID + ".transmutable")
.withStyle(ChatFormatting.DARK_GRAY));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package gripe._90.appliede.mixin.retardedfuckingtooltip;

import com.google.common.collect.BiMap;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import appeng.client.gui.me.common.Repo;
import appeng.menu.me.common.GridInventoryEntry;

import gripe._90.appliede.reporting.GridInventoryEMCEntry;

@Mixin(value = Repo.class, remap = false)
public abstract class RepoMixin {
@Shadow
@Final
private BiMap<Long, GridInventoryEntry> entries;

// spotless:off
@SuppressWarnings("UnreachableCode")
@Inject(
method = "handleUpdate(Lappeng/menu/me/common/GridInventoryEntry;)V",
at = @At(
value = "INVOKE",
target = "Lappeng/menu/me/common/GridInventoryEntry;<init>(JLappeng/api/stacks/AEKey;JJZ)V",
shift = At.Shift.AFTER),
cancellable = true,
locals = LocalCapture.CAPTURE_FAILEXCEPTION)
// spotless:on
private void setServerEntryTransmutable(
GridInventoryEntry serverEntry, CallbackInfo ci, GridInventoryEntry localEntry) {
ci.cancel();

var entry = new GridInventoryEntry(
serverEntry.getSerial(),
localEntry.getWhat(),
serverEntry.getStoredAmount(),
serverEntry.getRequestableAmount(),
serverEntry.isCraftable());
((GridInventoryEMCEntry) entry)
.appliede$setTransmutable(((GridInventoryEMCEntry) serverEntry).appliede$isTransmutable());
entries.put(serverEntry.getSerial(), entry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package gripe._90.appliede.reporting;

import net.minecraft.network.FriendlyByteBuf;

import appeng.api.stacks.AEKey;
import appeng.menu.me.common.GridInventoryEntry;

public interface GridInventoryEMCEntry {
boolean appliede$isTransmutable();

void appliede$setTransmutable(boolean extractable);

@SuppressWarnings("UnreachableCode")
static GridInventoryEntry readEntry(FriendlyByteBuf buffer) {
var serial = buffer.readVarLong();
var what = AEKey.readOptionalKey(buffer);
var storedAmount = buffer.readVarLong();
var requestableAmount = buffer.readVarLong();
var craftable = buffer.readBoolean();
var transmutable = buffer.readBoolean();

var entry = new GridInventoryEntry(serial, what, storedAmount, requestableAmount, craftable);
((GridInventoryEMCEntry) entry).appliede$setTransmutable(transmutable);
return entry;
}

static void writeEntry(FriendlyByteBuf buffer, GridInventoryEntry entry) {
buffer.writeVarLong(entry.getSerial());
AEKey.writeOptionalKey(buffer, entry.getWhat());
buffer.writeVarLong(entry.getStoredAmount());
buffer.writeVarLong(entry.getRequestableAmount());
buffer.writeBoolean(entry.isCraftable());
buffer.writeBoolean(((GridInventoryEMCEntry) entry).appliede$isTransmutable());
}
}
Loading

0 comments on commit 6426cad

Please sign in to comment.