Skip to content

Commit

Permalink
TinkerTabsScreen rework (#4841)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirderf1 committed Sep 5, 2022
1 parent c54c11c commit dd09c63
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,49 @@

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.core.BlockPos;
import net.minecraft.client.renderer.Rect2i;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.apache.commons.lang3.tuple.Pair;
import slimeknights.mantle.client.screen.ElementScreen;
import slimeknights.mantle.client.screen.MultiModuleScreen;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.common.network.TinkerNetwork;
import slimeknights.tconstruct.library.client.Icons;
import slimeknights.tconstruct.tables.block.ITabbedBlock;
import slimeknights.tconstruct.tables.client.inventory.module.SideInventoryScreen;
import slimeknights.tconstruct.tables.client.inventory.module.TinkerTabsScreen;
import slimeknights.tconstruct.tables.client.inventory.widget.TinkerTabsWidget;
import slimeknights.tconstruct.tables.menu.TabbedContainerMenu;
import slimeknights.tconstruct.tables.menu.module.SideInventoryContainer;
import slimeknights.tconstruct.tables.network.StationTabPacket;

import javax.annotation.Nullable;
import java.util.List;

public class BaseTabbedScreen<TILE extends BlockEntity, CONTAINER extends TabbedContainerMenu<TILE>> extends MultiModuleScreen<CONTAINER> {
protected static final Component COMPONENT_WARNING = TConstruct.makeTranslation("gui", "warning");
protected static final Component COMPONENT_ERROR = TConstruct.makeTranslation("gui", "error");

public static final ResourceLocation BLANK_BACK = TConstruct.getResource("textures/gui/blank.png");

@Nullable
protected final TILE tile;
protected final CONTAINER container;
protected TinkerTabsScreen tabsScreen;
protected TinkerTabsWidget tabsScreen;

public BaseTabbedScreen(CONTAINER container, Inventory playerInventory, Component title) {
super(container, playerInventory, title);
this.tile = container.getTile();
this.container = container;

this.tabsScreen = new TinkerTabsScreen(this, container, playerInventory, title);
this.addModule(this.tabsScreen);

if (this.tile != null) {
Level world = this.tile.getLevel();
}

if (world != null) {
for (Pair<BlockPos, BlockState> pair : container.stationBlocks) {
BlockState state = pair.getRight();
BlockPos blockPos = pair.getLeft();
ItemStack stack = state.getBlock().getCloneItemStack(state, null, world, blockPos, playerInventory.player);
this.tabsScreen.addTab(stack, blockPos);
}
}
@Override
protected void init() {
super.init();

// preselect the correct tab
for (int i = 0; i < this.tabsScreen.tabData.size(); i++) {
if (this.tabsScreen.tabData.get(i).equals(this.tile.getBlockPos())) {
this.tabsScreen.tabs.selected = i;
}
}
}
this.tabsScreen = addRenderableWidget(new TinkerTabsWidget(this));
}

@Nullable
public TILE getTileEntity() {
return this.tile;
}
Expand All @@ -84,30 +62,6 @@ protected void drawIconEmpty(PoseStack matrices, Slot slot, ElementScreen elemen
this.drawIcon(matrices, slot, element);
}

public void onTabSelection(int selection) {
if (selection < 0 || selection > this.tabsScreen.tabData.size()) {
return;
}

Level world = this.tile.getLevel();

if (world == null) {
return;
}

BlockPos pos = this.tabsScreen.tabData.get(selection);
BlockState state = world.getBlockState(pos);

if (state.getBlock() instanceof ITabbedBlock) {
// BlockEntity te = this.tile.getLevel().getBlockEntity(pos);
TinkerNetwork.getInstance().sendToServer(new StationTabPacket(pos));

// sound!
assert this.minecraft != null;
this.minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
}
}

public void error(Component message) {
}

Expand All @@ -118,7 +72,7 @@ public void updateDisplay() {
}

protected void addChestSideInventory(Inventory inventory) {
SideInventoryContainer<?> sideInventoryContainer = container.getSubContainer(SideInventoryContainer.class);
SideInventoryContainer<?> sideInventoryContainer = getMenu().getSubContainer(SideInventoryContainer.class);
if (sideInventoryContainer != null) {
// no title if missing one
Component sideInventoryName = TextComponent.EMPTY;
Expand All @@ -130,4 +84,17 @@ protected void addChestSideInventory(Inventory inventory) {
this.addModule(new SideInventoryScreen<>(this, sideInventoryContainer, inventory, sideInventoryName, sideInventoryContainer.getSlotCount(), sideInventoryContainer.getColumns()));
}
}

@Override
public List<Rect2i> getModuleAreas() {
List<Rect2i> areas = super.getModuleAreas();
areas.add(tabsScreen.getArea());
return areas;
}

@Override
protected boolean hasClickedOutside(double mouseX, double mouseY, int guiLeft, int guiTop, int mouseButton) {
return super.hasClickedOutside(mouseX, mouseY, guiLeft, guiTop, mouseButton)
&& !tabsScreen.isMouseOver(mouseX, mouseY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ protected void renderBg(PoseStack matrices, float partialTicks, int mouseX, int
this.drawRecipesBackground(matrices, mouseX, mouseY, this.cornerX + 51, this.cornerY + 15);

// draw slot icons
this.drawIconEmpty(matrices, this.container.getPatternSlot(), Icons.PATTERN);
this.drawIconEmpty(matrices, this.container.getInputSlot(), Icons.INGOT);
this.drawIconEmpty(matrices, this.getMenu().getPatternSlot(), Icons.PATTERN);
this.drawIconEmpty(matrices, this.getMenu().getInputSlot(), Icons.INGOT);
this.drawRecipesItems(matrices, this.cornerX + 51, this.cornerY + 15);

super.renderBg(matrices, partialTicks, mouseX, mouseY);
Expand Down Expand Up @@ -267,10 +267,10 @@ public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
// handle button click
int index = getButtonAt((int)mouseX, (int)mouseY);
assert this.minecraft != null && this.minecraft.player != null;
if (index >= 0 && this.container.clickMenuButton(this.minecraft.player, index)) {
if (index >= 0 && this.getMenu().clickMenuButton(this.minecraft.player, index)) {
this.minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_STONECUTTER_SELECT_RECIPE, 1.0F));
assert this.minecraft.gameMode != null;
this.minecraft.gameMode.handleInventoryButtonClick(this.container.containerId, index);
this.minecraft.gameMode.handleInventoryButtonClick(this.getMenu().containerId, index);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void onClose() {
public void updateLayout() {
int stillFilled = 0;
for (int i = 0; i <= maxInputs; i++) {
Slot slot = this.container.getSlot(i);
Slot slot = this.getMenu().getSlot(i);
LayoutSlot layoutSlot = currentLayout.getSlot(i);
if (layoutSlot.isHidden()) {
// put the position in the still filled line
Expand Down Expand Up @@ -237,7 +237,7 @@ public void updateDisplay() {
return;
}

ItemStack toolStack = this.container.getResult();
ItemStack toolStack = this.getMenu().getResult();

// if we have a message, display instead of refreshing the tool
ValidatedResult currentError = tile.getCurrentError();
Expand All @@ -248,7 +248,7 @@ public void updateDisplay() {

// normal refresh
if (toolStack.isEmpty()) {
toolStack = this.container.getSlot(TINKER_SLOT).getItem();
toolStack = this.getMenu().getSlot(TINKER_SLOT).getItem();
}

// if the contained stack is modifiable, display some information
Expand Down Expand Up @@ -321,7 +321,7 @@ public void updateDisplay() {
if (!layout.isHidden() && !key.isEmpty()) {
hasComponents = true;
MutableComponent textComponent = new TextComponent(" * ");
ItemStack slotStack = this.container.getSlot(i).getItem();
ItemStack slotStack = this.getMenu().getSlot(i).getItem();
if (!layout.isValid(slotStack)) {
textComponent.withStyle(ChatFormatting.RED);
}
Expand Down Expand Up @@ -411,18 +411,18 @@ protected void renderBg(PoseStack matrices, float partialTicks, int mouseX, int
// slot backgrounds, are transparent
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 0.28f);
if (!this.currentLayout.getToolSlot().isHidden()) {
Slot slot = this.container.getSlot(TINKER_SLOT);
Slot slot = this.getMenu().getSlot(TINKER_SLOT);
SLOT_BACKGROUND.draw(matrices, x + this.cornerX + slot.x - 1, y + this.cornerY + slot.y - 1);
}
for (int i = 0; i < this.activeInputs; i++) {
Slot slot = this.container.getSlot(i + INPUT_SLOT);
Slot slot = this.getMenu().getSlot(i + INPUT_SLOT);
SLOT_BACKGROUND.draw(matrices, x + this.cornerX + slot.x - 1, y + this.cornerY + slot.y - 1);
}

// slot borders, are opaque
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
for (int i = 0; i <= maxInputs; i++) {
Slot slot = this.container.getSlot(i);
Slot slot = this.getMenu().getSlot(i);
if ((slot instanceof TinkerStationSlot && (!((TinkerStationSlot) slot).isDormant() || slot.hasItem()))) {
SLOT_BORDER.draw(matrices, x + this.cornerX + slot.x - 1, y + this.cornerY + slot.y - 1);
}
Expand Down Expand Up @@ -461,7 +461,7 @@ protected void renderBg(PoseStack matrices, float partialTicks, int mouseX, int
// render slot background icons
RenderSystem.setShaderTexture(0, InventoryMenu.BLOCK_ATLAS);
for (int i = 0; i <= maxInputs; i++) {
Slot slot = this.container.getSlot(i);
Slot slot = this.getMenu().getSlot(i);
if (!slot.hasItem()) {
Pattern icon = currentLayout.getSlot(i).getIcon();
if (icon != null) {
Expand Down

This file was deleted.

0 comments on commit dd09c63

Please sign in to comment.