Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Quantum Storage to MUI2 #2554

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2690429
port chest to mui2
ghzdude May 29, 2024
2251a33
remove old ui
ghzdude May 29, 2024
94471b8
more work on ui
ghzdude May 29, 2024
c6b746b
begin port of quantum tank
ghzdude May 30, 2024
3406ff9
small fixes
ghzdude May 30, 2024
42f5c65
more ui things
ghzdude May 31, 2024
63f387e
more ui work
ghzdude Jun 1, 2024
332623a
add rendering widgets
ghzdude Jun 2, 2024
cea9aa2
remove commented code
ghzdude Jun 11, 2024
8840880
minor fixes + spotless
ghzdude Jun 11, 2024
df83038
fix rebase
ghzdude Jul 30, 2024
a1ebcfa
format numbers better
ghzdude Jul 30, 2024
2701ffc
The Great Code Migration of 2024
ghzdude Oct 26, 2024
c03b73c
add locking to qchest
ghzdude Oct 26, 2024
d137ffd
start work on qchest item renderer
ghzdude Oct 26, 2024
a5493eb
work on qchest ui a bit
ghzdude Dec 28, 2024
bc1c88f
introduce mapped synchandler
ghzdude Jan 3, 2025
a43bb12
fix display name
ghzdude Jan 3, 2025
401a7a4
initial work on creative chest
ghzdude Jan 4, 2025
82eeea0
spotless
ghzdude Jan 4, 2025
ac44094
creative chest mostly done
ghzdude Jan 5, 2025
53e7835
port creative tank
ghzdude Jan 5, 2025
44a1fe9
switch to phantom item/fluid slot
ghzdude Jan 6, 2025
978f1eb
fix small issue with qchest
ghzdude Jan 6, 2025
e82a622
spotless
ghzdude Jan 6, 2025
43cb2ae
fix rebase
ghzdude Jan 6, 2025
ca6ed6f
use our fluid slots
ghzdude Jan 6, 2025
488fc40
move draw item stack into RenderUtil
ghzdude Jan 7, 2025
8ecb0a0
initial gt item slot impl
ghzdude Jan 7, 2025
4a7bd8f
fix compile
ghzdude Jan 7, 2025
42b5dc6
allow extract from input slot
ghzdude Jan 7, 2025
fb676a9
qtank is not phantom
ghzdude Jan 7, 2025
764e15b
simplify item render
ghzdude Jan 7, 2025
5630c26
make drawing item count configurable
ghzdude Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
initial gt item slot impl
ghzdude committed Jan 7, 2025
commit 8ecb0a0336647a6f994a9153d08590f1328f78a3
67 changes: 67 additions & 0 deletions src/main/java/gregtech/api/mui/sync/GTItemSyncHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package gregtech.api.mui.sync;

import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.items.ItemHandlerHelper;

import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import com.cleanroommc.modularui.widgets.slot.ModularSlot;

import java.io.IOException;

public class GTItemSyncHandler extends SyncHandler {

private final ModularSlot slot;
private ItemStack lastStoredItem;
private ItemStack lastStoredPhantomItem = ItemStack.EMPTY;

public GTItemSyncHandler(ModularSlot slot) {
this.slot = slot;
}

@Override
public void init(String key, PanelSyncManager syncManager) {
super.init(key, syncManager);
// noinspection UnstableApiUsage
getSyncManager().getContainer().registerSlot(getSyncManager().getPanelName(), this.slot);
this.lastStoredItem = getSlot().getStack().copy();
// if (isPhantom() && !getSlot().getStack().isEmpty()) {
// this.lastStoredPhantomItem = getSlot().getStack().copy();
// this.lastStoredPhantomItem.setCount(1);
// }
}

@Override
public void detectAndSendChanges(boolean init) {
ItemStack itemStack = getSlot().getStack();
if (itemStack.isEmpty() && this.lastStoredItem.isEmpty()) return;
boolean onlyAmountChanged = false;
if (init ||
!ItemHandlerHelper.canItemStacksStack(this.lastStoredItem, itemStack) ||
(onlyAmountChanged = itemStack.getCount() != this.lastStoredItem.getCount())) {
getSlot().onSlotChangedReal(itemStack, onlyAmountChanged, false, init);
if (onlyAmountChanged) {
this.lastStoredItem.setCount(itemStack.getCount());
} else {
this.lastStoredItem = itemStack.isEmpty() ? ItemStack.EMPTY : itemStack.copy();
}
final boolean finalOnlyAmountChanged = onlyAmountChanged;
syncToClient(1, buffer -> {
buffer.writeBoolean(finalOnlyAmountChanged);
buffer.writeItemStack(itemStack);
buffer.writeBoolean(init);
});
}
}

public ModularSlot getSlot() {
return slot;
}

@Override
public void readOnClient(int id, PacketBuffer buf) throws IOException {}

@Override
public void readOnServer(int id, PacketBuffer buf) throws IOException {}
}
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
import gregtech.api.util.TextFormattingUtil;
import gregtech.client.renderer.texture.Textures;
import gregtech.client.renderer.texture.custom.QuantumStorageRenderer;
import gregtech.common.mui.widget.GTItemSlot;

import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.I18n;
@@ -45,7 +46,6 @@
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import com.cleanroommc.modularui.widgets.ItemSlot;
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
@@ -317,7 +317,8 @@ protected void createWidgets(ModularPanel mainPanel, PanelSyncManager syncManage
textWidget -> !virtualItemStack.isEmpty(),
() -> TextFormattingUtil.formatNumbers(itemsStoredInside)))
// todo make and use GT item slot for special behavior
.child(new ItemSlot()
.child(new GTItemSlot()
.showTooltip(false)
// todo disable tooltip
.slot(new ModularSlot(itemInventory, 0)
.accessibility(false, false))
185 changes: 185 additions & 0 deletions src/main/java/gregtech/common/mui/widget/GTItemSlot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package gregtech.common.mui.widget;

import gregtech.client.utils.RenderUtil;

import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;

import com.cleanroommc.modularui.api.ITheme;
import com.cleanroommc.modularui.api.widget.IVanillaSlot;
import com.cleanroommc.modularui.api.widget.Interactable;
import com.cleanroommc.modularui.drawable.GuiDraw;
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
import com.cleanroommc.modularui.integration.jei.JeiIngredientProvider;
import com.cleanroommc.modularui.screen.ClientScreenHandler;
import com.cleanroommc.modularui.screen.ModularScreen;
import com.cleanroommc.modularui.screen.RichTooltip;
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
import com.cleanroommc.modularui.theme.WidgetSlotTheme;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.MouseData;
import com.cleanroommc.modularui.value.sync.ItemSlotSH;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import com.cleanroommc.modularui.widget.Widget;
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class GTItemSlot extends Widget<GTItemSlot>
implements IVanillaSlot, Interactable, JeiGhostIngredientSlot<ItemStack>,
JeiIngredientProvider {

private boolean showTooltip = true;
private ItemSlotSH syncHandler;

public GTItemSlot() {
tooltip().setAutoUpdate(true);
// .setHasTitleMargin(true);
tooltipBuilder(tooltip -> {
if (!isSynced()) return;
ItemStack stack = getSlot().getStack();
if (stack.isEmpty()) return;
tooltip.addFromItem(stack);
});
}

@Override
public void onInit() {
if (getScreen().isOverlay()) {
throw new IllegalStateException("Overlays can't have slots!");
}
size(18);
getContext().getJeiSettings().addJeiGhostIngredientSlot(this);
}

public GTItemSlot showTooltip(boolean showTooltip) {
this.showTooltip = showTooltip;
return getThis();
}

@SuppressWarnings("UnstableApiUsage")
public GTItemSlot slot(ModularSlot slot) {
this.syncHandler = new ItemSlotSH(slot);
setSyncHandler(this.syncHandler);
return getThis();
}

public GTItemSlot slot(IItemHandlerModifiable itemHandler, int index) {
return slot(new ModularSlot(itemHandler, index));
}

@Override
public @NotNull Result onMousePressed(int mouseButton) {
if (this.syncHandler.isPhantom()) {
MouseData mouseData = MouseData.create(mouseButton);
this.syncHandler.syncToServer(2, mouseData::writeToPacket);
} else {
ClientScreenHandler.clickSlot();
}
return Result.SUCCESS;
}

@Override
public boolean onMouseRelease(int mouseButton) {
if (!this.syncHandler.isPhantom()) {
ClientScreenHandler.releaseSlot();
}
return true;
}

@Override
public boolean onMouseScroll(ModularScreen.UpOrDown scrollDirection, int amount) {
if (this.syncHandler.isPhantom()) {
MouseData mouseData = MouseData.create(scrollDirection.modifier);
this.syncHandler.syncToServer(3, mouseData::writeToPacket);
return true;
}
return false;
}

@Override
public void onMouseDrag(int mouseButton, long timeSinceClick) {
ClientScreenHandler.dragSlot(timeSinceClick);
}

@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
this.syncHandler = castIfTypeElseNull(syncHandler, ItemSlotSH.class);
return this.syncHandler != null;
}

@Override
public void onUpdate() {
super.onUpdate();
boolean shouldBeEnabled = areAncestorsEnabled();
if (shouldBeEnabled != getSlot().isEnabled()) {
this.syncHandler.setEnabled(shouldBeEnabled, true);
}
}

@Override
public void drawForeground(ModularGuiContext context) {
RichTooltip tooltip = getTooltip();
if (showTooltip && tooltip != null && isHoveringFor(tooltip.getShowUpTimer())) {
tooltip.draw(getContext(), getSlot().getStack());
}
}

@Override
public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {
if (this.syncHandler == null) return;
RenderHelper.enableGUIStandardItemLighting();
// todo draw slot
RenderUtil.drawItemStack(getSlot().getStack(), 0, 0, null);
// drawSlot(getSlot());
RenderHelper.enableStandardItemLighting();
GlStateManager.disableLighting();
if (isHovering()) {
GlStateManager.colorMask(true, true, true, false);
GuiDraw.drawRect(1, 1, 16, 16, getSlotHoverColor());
GlStateManager.colorMask(true, true, true, true);
}
}

@Override
public WidgetSlotTheme getWidgetThemeInternal(ITheme theme) {
return theme.getItemSlotTheme();
}

public int getSlotHoverColor() {
WidgetTheme theme = getWidgetTheme(getContext().getTheme());
if (theme instanceof WidgetSlotTheme slotTheme) {
return slotTheme.getSlotHoverColor();
}
return ITheme.getDefault().getItemSlotTheme().getSlotHoverColor();
}

@Override
public Slot getVanillaSlot() {
return getSlot();
}

public ModularSlot getSlot() {
return this.syncHandler.getSlot();
}

@Override
public void setGhostIngredient(@NotNull ItemStack ingredient) {
if (this.syncHandler.isPhantom()) {
this.syncHandler.updateFromClient(ingredient);
}
}

@Override
public @Nullable ItemStack castGhostIngredientIfValid(@NotNull Object ingredient) {
return this.syncHandler.isPhantom() && ingredient instanceof ItemStack itemStack ? itemStack : null;
}

@Override
public @Nullable Object getIngredient() {
return getSlot().getStack();
}
}