Skip to content

Commit

Permalink
start of gui stuff and fluid/slot components
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungOnionMC committed Dec 8, 2024
1 parent 0a6d78e commit c9b8b23
Show file tree
Hide file tree
Showing 18 changed files with 710 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,13 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player
var result = interactedMachine.onUse(state, world, pos, player, hand, hit);
if (result != InteractionResult.PASS) return result;
}
if (shouldOpenUi && machine instanceof IUIMachine uiMachine &&
if(shouldOpenUi && machine instanceof IUIMachine2 uiMachine && canOpenOwnerMachine(player, machine.getHolder())) {

}
/*if (shouldOpenUi && machine instanceof IUIMachine uiMachine &&
canOpenOwnerMachine(player, machine.getHolder())) {
return uiMachine.tryToOpenUI(player, hand, hit);
}
}*/
return shouldOpenUi ? InteractionResult.PASS : InteractionResult.CONSUME;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public MachineUIFactory() {
@Override
protected ModularUI createUITemplate(MetaMachine holder, Player entityPlayer) {
if (holder instanceof IUIMachine machine) {
//return null;
return machine.createUI(entityPlayer);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gregtechceu.gtceu.api.machine.feature;

import com.gregtechceu.gtceu.api.ui.core.UIAdapter;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;

public interface IUIContainer {

IUIContainer EMPTY = new IUIContainer() {
@Override
public UIAdapter<?> createMenu(Player player) {
return null;
}

@Override
public boolean isInvalid() { return false; }

@Override
public boolean isClientSide() { return false; }

@Override
public void markDirty() {}
};

UIAdapter<?> createMenu(Player player);

boolean isInvalid();

boolean isClientSide();

void markDirty();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

import com.gregtechceu.gtceu.api.gui.factory.MachineUIFactory;

import com.gregtechceu.gtceu.api.ui.UIContainer;
import com.lowdragmc.lowdraglib.LDLib;
import com.lowdragmc.lowdraglib.gui.modular.IUIHolder;

import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

/**
* @author KilaBash
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.gregtechceu.gtceu.api.machine.feature;

import com.gregtechceu.gtceu.api.ui.UIContainer;
import com.lowdragmc.lowdraglib.LDLib;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

public interface IUIMachine2 extends MenuProvider, IMachineFeature {

default boolean shouldOpenUI(Player player, InteractionHand hand, BlockHitResult hit) {
return true;
}

default InteractionResult tryToOpenUI(Player player, InteractionHand hand, BlockHitResult result) {
if(this.shouldOpenUI(player, hand, result)) {
if(player instanceof ServerPlayer serverPlayer) {
NetworkHooks.openScreen(serverPlayer, this);
}
} else {
return InteractionResult.PASS;
}
return InteractionResult.sidedSuccess(player.level().isClientSide);
}

default boolean isRemote() {
var level = self().getLevel();
return level == null ? LDLib.isRemote() : level.isClientSide;
}

@Nullable
@Override
default AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) {
return new UIContainer(i, inventory);
}

@Override
default Component getDisplayName() {
return Component.empty();
}
}
204 changes: 204 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/api/ui/UIContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package com.gregtechceu.gtceu.api.ui;

import com.gregtechceu.gtceu.api.ui.base.BaseContainerScreen;
import com.gregtechceu.gtceu.api.ui.component.ItemComponent;
import com.gregtechceu.gtceu.api.ui.component.SlotComponent;
import com.gregtechceu.gtceu.api.ui.core.UIAdapter;
import com.gregtechceu.gtceu.api.ui.core.UIComponent;
import lombok.Setter;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class UIContainer extends AbstractContainerMenu {

public final static MenuType<UIContainer> DEFAULT_TYPE = new MenuType<>(UIContainer::new, FeatureFlags.DEFAULT_FLAGS);

private Inventory playerInv;
@Setter
private UIAdapter<?> adapter;

protected UIContainer(@Nullable MenuType<?> menuType, int containerId) {
super(menuType, containerId);
}

public UIContainer(int containerId, Inventory playerInv) {
super(DEFAULT_TYPE, containerId);
this.playerInv = playerInv;
}

public UIContainer(int containerId, Inventory playerInv, UIAdapter<?> adapter) {
super(DEFAULT_TYPE, containerId);
this.playerInv = playerInv;
this.adapter = adapter;
}

public void addAllSlots() {
var root = adapter.rootComponent;
root.forEachDescendant(child -> {
if(child instanceof SlotComponent slot) {
addSlot(slot.getSlot());
};
});
int slotSize = this.slots.size();


for(int i1 = 0; i1 < 3; i1++) {
for(int j1 = 0; j1 < 9; j1++) {
this.addSlot(new Slot(playerInv, j1 + i1 * 9 + 9, 8 + j1 * 18, 103 + i1 * 18));
}
}

for(int i1 = 0; i1 < 9; i1++) {
this.addSlot(new Slot(playerInv, i1, 8 + i1 * 18, 161));
}



}

@Override
public ItemStack quickMoveStack(Player player, int i) {
if(playerInv.player.level().isClientSide) {
return ItemStack.EMPTY;
}

final Slot clickedSlot = this.slots.get(i);
boolean playerSide = isPlayerSideSlot(clickedSlot);

//if(clickedSlot.isActive()) // todo disabled slots

if(clickedSlot.hasItem()) {
ItemStack stack = clickedSlot.getItem();

final List<Slot> selectedSlots = new ArrayList<>();

if(playerSide) {
for(Slot c : this.slots) {
if(!isPlayerSideSlot(c) && c.mayPlace(stack)) {
selectedSlots.add(c);
}
}
} else {
for(Slot c : this.slots) {
if(isPlayerSideSlot(c) && c.mayPlace(stack)) {
selectedSlots.add(c);
}
}
}

if(!stack.isEmpty()) {
for(Slot d : selectedSlots){
if(d.mayPlace(stack) && d.hasItem() && movedFullStack(clickedSlot, stack, d)) {
return ItemStack.EMPTY;
}
}

for(Slot d : selectedSlots) {
if(d.mayPlace(stack)) {
if(d.hasItem()) {
if(movedFullStack(clickedSlot, stack, d)) {
return ItemStack.EMPTY;
}
}
else {
int maxSize = stack.getMaxStackSize();
if (maxSize > d.getMaxStackSize()) {
maxSize = d.getMaxStackSize();
}

final ItemStack tmp = stack.copy();
if(tmp.getCount() > maxSize) {
tmp.setCount(maxSize);
}

stack.setCount( stack.getCount() - tmp.getCount());
d.set(tmp);
if(stack.getCount() <= 0) {
clickedSlot.set(ItemStack.EMPTY);
d.setChanged();

broadcastChanges();
return ItemStack.EMPTY;
}
else {
broadcastChanges();
}
}
}
}
}
clickedSlot.set(!stack.isEmpty() ? stack : ItemStack.EMPTY);
}
broadcastChanges();
return ItemStack.EMPTY;
}

private boolean isPlayerSideSlot(Slot s) {
return s.container == this.playerInv;
}

private boolean movedFullStack(Slot clickedSlot, ItemStack stack, Slot dest) {
final ItemStack t = dest.getItem().copy();

if(ItemStack.isSameItemSameTags(t, stack)) {
int maxSize = t.getMaxStackSize();
if(maxSize > dest.getMaxStackSize()) {
maxSize = dest.getMaxStackSize();
}

int placeable = maxSize - t.getCount();
if(placeable > 0) {
if(stack.getCount() < placeable) {
placeable = stack.getCount();
}

t.setCount(t.getCount() + placeable);
stack.setCount(stack.getCount() - placeable);

dest.set(t);

if(stack.getCount() <=0) {
clickedSlot.set(ItemStack.EMPTY);
dest.setChanged();

broadcastChanges();
return true;
}
else {
broadcastChanges();
}
}
}
return false;
}

@Override
public boolean stillValid(Player player) {
return false;
}

@Override
public void gtceu$attachToPlayer(Player player) {

}

@Override
public void gtceu$readPropertySync(FriendlyByteBuf packet) {

}

@Override
public void gtceu$handlePacket(FriendlyByteBuf packet, boolean clientbound) {

}
}
37 changes: 37 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/api/ui/UIFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.gregtechceu.gtceu.api.ui;

import com.google.common.base.Preconditions;
import com.gregtechceu.gtceu.api.ui.core.UIAdapter;
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.Nameable;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.common.extensions.IForgeMenuType;
import net.minecraftforge.network.NetworkHooks;

public abstract class UIFactory<T> {


public boolean tryOpenUI(T holder, Player player) {
if(!(player instanceof ServerPlayer))
return false;
UIAdapter<?> adapter = createAdapter(holder, player);
if(adapter == null) return false;


FriendlyByteBuf serializedHolder = new FriendlyByteBuf(Unpooled.buffer());


/*NetworkHooks.openScreen((ServerPlayer)player, new SimpleMenuProvider((id, inv, p) -> ));*/
return false;
}

protected abstract UIAdapter<?> createAdapter(T holder, Player player);
}
Loading

0 comments on commit c9b8b23

Please sign in to comment.