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

Refactor Container customiztion and phantom slots & crafting grids #118

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 4 additions & 3 deletions src/main/java/com/cleanroommc/modularui/api/IGuiHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.cleanroommc.modularui.factory.GuiData;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.screen.ModularScreen;

import com.cleanroommc.modularui.screen.UISettings;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;

import net.minecraftforge.fml.relauncher.Side;
Expand All @@ -19,7 +19,7 @@ public interface IGuiHolder<T extends GuiData> {
* Only called on client side.
*
* @param data information about the creation context
* @param mainPanel the panel created in {@link #buildUI(GuiData, PanelSyncManager)}
* @param mainPanel the panel created in {@link #buildUI(GuiData, PanelSyncManager, UISettings)}
* @return a modular screen instance with the given panel
*/
@SideOnly(Side.CLIENT)
Expand All @@ -34,6 +34,7 @@ default ModularScreen createScreen(T data, ModularPanel mainPanel) {
*
* @param data information about the creation context
* @param syncManager sync handler where widget sync handlers should be registered
* @param settings settings which apply to the whole ui and not just this panel
*/
ModularPanel buildUI(T data, PanelSyncManager syncManager);
ModularPanel buildUI(T data, PanelSyncManager syncManager, UISettings settings);
}
32 changes: 26 additions & 6 deletions src/main/java/com/cleanroommc/modularui/api/UIFactory.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.cleanroommc.modularui.api;

import com.cleanroommc.modularui.factory.GuiData;
import com.cleanroommc.modularui.screen.GuiContainerWrapper;
import com.cleanroommc.modularui.screen.ModularContainer;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.screen.ModularScreen;
import com.cleanroommc.modularui.screen.*;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;

import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -36,16 +33,17 @@ public interface UIFactory<D extends GuiData> {
*
* @param guiData gui data
* @param syncManager sync manager
* @param settings ui settings
* @return new main panel
*/
@ApiStatus.OverrideOnly
ModularPanel createPanel(D guiData, PanelSyncManager syncManager);
ModularPanel createPanel(D guiData, PanelSyncManager syncManager, UISettings settings);

/**
* Creates the screen for the GUI. Is only called on client side.
*
* @param guiData gui data
* @param mainPanel main panel created in {@link #createPanel(GuiData, PanelSyncManager)}
* @param mainPanel main panel created in {@link #createPanel(GuiData, PanelSyncManager, UISettings)}
* @return new main panel
*/
@SideOnly(Side.CLIENT)
Expand All @@ -68,6 +66,28 @@ default IMuiScreen createScreenWrapper(ModularContainer container, ModularScreen
return new GuiContainerWrapper(container, screen);
}

/**
* The default container supplier. This is called when no custom container in {@link UISettings} is set.
*
* @return new container instance
*/
default ModularContainer createContainer() {
return new ModularContainer();
}

/**
* A default function to check if the current interacting player can interact with the ui. If not overridden on {@link UISettings},
* then this is called every tick while a UI opened by this factory is open. Once this function returns false, the UI is immediately
* closed.
*
* @param player current interacting player
* @param guiData gui data of the current ui
* @return if the player can interact with the player.
*/
default boolean canInteractWith(EntityPlayer player, D guiData) {
return player == guiData.getPlayer();
}

/**
* Writes the gui data to a buffer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
/**
* Marks a {@link IWidget}, that this is a vanilla item slot.
*/
@FunctionalInterface
public interface IVanillaSlot {

/**
* @return the item slot of this widget
*/
Slot getVanillaSlot();

boolean handleAsVanillaSlot();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.cleanroommc.modularui.core.mixin;

import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;

import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;

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

@Mixin(InventoryCrafting.class)
public interface InventoryCraftingAccessor {

@Accessor
NonNullList<ItemStack> getStackList();

@Accessor
Container getEventHandler();
}
66 changes: 63 additions & 3 deletions src/main/java/com/cleanroommc/modularui/drawable/ItemDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.cleanroommc.modularui.utils.JsonHelper;
import com.cleanroommc.modularui.widget.Widget;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
Expand All @@ -17,18 +19,46 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.NoSuchElementException;

public class ItemDrawable implements IDrawable {

private ItemStack item = ItemStack.EMPTY;

public ItemDrawable() {
}
public ItemDrawable() {}

public ItemDrawable(@NotNull ItemStack item) {
this.item = item;
setItem(item);
}

public ItemDrawable(@NotNull Item item) {
setItem(item);
}

public ItemDrawable(@NotNull Item item, int meta) {
setItem(item, meta);
}

public ItemDrawable(@NotNull Item item, int meta, int amount) {
setItem(item, meta, amount);
}

public ItemDrawable(@NotNull Item item, int meta, int amount, @Nullable NBTTagCompound nbt) {
setItem(item, meta, amount, nbt);
}

public ItemDrawable(@NotNull Block item) {
setItem(item);
}

public ItemDrawable(@NotNull Block item, int meta) {
setItem(item, meta);
}

public ItemDrawable(@NotNull Block item, int meta, int amount) {
setItem(new ItemStack(item, amount, meta));
}

@SideOnly(Side.CLIENT)
Expand All @@ -52,6 +82,36 @@ public ItemDrawable setItem(@NotNull ItemStack item) {
return this;
}

public ItemDrawable setItem(@NotNull Item item) {
return setItem(item, 0, 1, null);
}

public ItemDrawable setItem(@NotNull Item item, int meta) {
return setItem(item, meta, 1, null);
}

public ItemDrawable setItem(@NotNull Item item, int meta, int amount) {
return setItem(item, meta, amount, null);
}

public ItemDrawable setItem(@NotNull Item item, int meta, int amount, @Nullable NBTTagCompound nbt) {
ItemStack itemStack = new ItemStack(item, amount, meta);
itemStack.setTagCompound(nbt);
return setItem(itemStack);
}

public ItemDrawable setItem(@NotNull Block item) {
return setItem(item, 0, 1);
}

public ItemDrawable setItem(@NotNull Block item, int meta) {
return setItem(item, meta, 1);
}

public ItemDrawable setItem(@NotNull Block item, int meta, int amount) {
return setItem(new ItemStack(item, amount, meta));
}

public static ItemDrawable ofJson(JsonObject json) {
String itemS = JsonHelper.getString(json, null, "item");
if (itemS == null) throw new JsonParseException("Item property not found!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.screen.ModularScreen;

import com.cleanroommc.modularui.screen.UISettings;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;

import org.jetbrains.annotations.NotNull;
Expand All @@ -28,9 +29,9 @@ protected AbstractUIFactory(String name) {
public abstract IGuiHolder<T> getGuiHolder(T data);

@Override
public ModularPanel createPanel(T guiData, PanelSyncManager syncManager) {
public ModularPanel createPanel(T guiData, PanelSyncManager syncManager, UISettings settings) {
IGuiHolder<T> guiHolder = Objects.requireNonNull(getGuiHolder(guiData), "Gui holder must not be null!");
return guiHolder.buildUI(guiData, syncManager);
return guiHolder.buildUI(guiData, syncManager, settings);
}

@Override
Expand Down
42 changes: 30 additions & 12 deletions src/main/java/com/cleanroommc/modularui/factory/ClientGUI.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.cleanroommc.modularui.factory;

import com.cleanroommc.modularui.api.MCHelper;
import com.cleanroommc.modularui.screen.ContainerCustomizer;
import com.cleanroommc.modularui.screen.JeiSettingsImpl;
import com.cleanroommc.modularui.screen.ModularContainer;
import com.cleanroommc.modularui.screen.ModularScreen;
import com.cleanroommc.modularui.screen.UISettings;

import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.fml.relauncher.Side;
Expand All @@ -12,6 +13,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

/**
* Helper class to open client only GUIs. This class is safe to use inside a Modular GUI.
* Direct calls to {@link net.minecraft.client.Minecraft#displayGuiScreen(GuiScreen)} are redirected to this class if
Expand All @@ -29,7 +32,7 @@ private ClientGUI() {
* @param screen new modular screen
*/
public static void open(@NotNull ModularScreen screen) {
open(screen, new JeiSettingsImpl(), null);
open(screen, new UISettings());
}

/**
Expand All @@ -40,30 +43,45 @@ public static void open(@NotNull ModularScreen screen) {
* @param jeiSettings custom jei settings
*/
public static void open(@NotNull ModularScreen screen, @NotNull JeiSettingsImpl jeiSettings) {
GuiManager.openScreen(screen, jeiSettings, null);
GuiManager.openScreen(screen, new UISettings(jeiSettings));
}

/**
* Opens a modular screen on the next client tick with custom jei settings.
* It needs to be opened in next tick, because we might break the current GUI if we open it now.
*
* @param screen new modular screen
* @param container custom container
*/
public static void open(@NotNull ModularScreen screen, @Nullable Supplier<ModularContainer> container) {
UISettings settings = new UISettings();
settings.customContainer(container);
GuiManager.openScreen(screen, settings);
}

/**
* Opens a modular screen on the next client tick with custom jei settings.
* It needs to be opened in next tick, because we might break the current GUI if we open it now.
*
* @param screen new modular screen
* @param containerCustomizer container customizer
* @param screen new modular screen
* @param jeiSettings custom jei settings
* @param container custom container
*/
public static void open(@NotNull ModularScreen screen, @NotNull ContainerCustomizer containerCustomizer) {
GuiManager.openScreen(screen, new JeiSettingsImpl(), containerCustomizer);
public static void open(@NotNull ModularScreen screen, @NotNull JeiSettingsImpl jeiSettings, @Nullable Supplier<ModularContainer> container) {
UISettings settings = new UISettings(jeiSettings);
settings.customContainer(container);
GuiManager.openScreen(screen, settings);
}

/**
* Opens a modular screen on the next client tick with custom jei settings.
* It needs to be opened in next tick, because we might break the current GUI if we open it now.
*
* @param screen new modular screen
* @param jeiSettings custom jei settings
* @param containerCustomizer container customizer
* @param screen new modular screen
* @param settings ui settings
*/
public static void open(@NotNull ModularScreen screen, @NotNull JeiSettingsImpl jeiSettings, @Nullable ContainerCustomizer containerCustomizer) {
GuiManager.openScreen(screen, jeiSettings, containerCustomizer);
public static void open(@NotNull ModularScreen screen, @NotNull UISettings settings) {
GuiManager.openScreen(screen, settings);
}

/**
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/cleanroommc/modularui/factory/GuiData.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.cleanroommc.modularui.factory;

import com.cleanroommc.modularui.api.JeiSettings;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.screen.JeiSettingsImpl;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
Expand All @@ -12,7 +10,6 @@
/**
* This class and subclasses are holding necessary data to find the exact same GUI on client and server.
* For example, if the GUI was opened by right-clicking a TileEntity, then this data needs a world and a block pos.
* Additionally, this can be used to configure JEI via {@link #getJeiSettings()}.
* <p>
* Also see {@link PosGuiData} (useful for TileEntities), {@link SidedPosGuiData} (useful for covers from GregTech) and
* {@link HandGuiData} (useful for guis opened by interacting with an item in the players hand) for default implementations.
Expand All @@ -21,7 +18,6 @@
public class GuiData {

private final EntityPlayer player;
private JeiSettings jeiSettings;

public GuiData(EntityPlayer player) {
this.player = Objects.requireNonNull(player);
Expand All @@ -42,19 +38,4 @@ public ItemStack getMainHandItem() {
public ItemStack getOffHandItem() {
return this.player.getHeldItemOffhand();
}

public JeiSettings getJeiSettings() {
if (this.jeiSettings == null) {
throw new IllegalStateException("Not yet initialised!");
}
return this.jeiSettings;
}

final JeiSettingsImpl getJeiSettingsImpl() {
return (JeiSettingsImpl) this.jeiSettings;
}

final void setJeiSettings(JeiSettings jeiSettings) {
this.jeiSettings = Objects.requireNonNull(jeiSettings);
}
}
Loading