-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start of gui stuff and fluid/slot components
- Loading branch information
1 parent
0a6d78e
commit c9b8b23
Showing
18 changed files
with
710 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/gregtechceu/gtceu/api/machine/feature/IUIContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/gregtechceu/gtceu/api/machine/feature/IUIMachine2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
204
src/main/java/com/gregtechceu/gtceu/api/ui/UIContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.