Skip to content

Commit

Permalink
refactor(mod): implement ContainerListener in BaseContainerScreen
Browse files Browse the repository at this point in the history
- Add ContainerListener implementation to BaseContainerScreen
- Update BaseMenu to use BlockEntity type- Modify CompressedChestMenu and CompressedChestTile to use new BaseMenu
- Update CompressorMenu and CompressorTile to use new BaseMenu
- Adjust ExtremeAnvilMenu and ExtremeAnvilTile to use new BaseMenu
- Modify InfinityChestMenu and InfinityChestTile to use new BaseMenu
- Update related screen classes to use new BaseContainerScreen methods
- Remove redundant methods and simplify existing ones across multiple classes
  • Loading branch information
cnlimiter committed Dec 25, 2024
1 parent fba8505 commit 6e59016
Show file tree
Hide file tree
Showing 45 changed files with 234 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerListener;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.text.DecimalFormat;
Expand All @@ -17,11 +19,15 @@
* Date: 2022/4/2 11:37
* Version: 1.0
*/
public abstract class BaseContainerScreen<T extends AbstractContainerMenu> extends AbstractContainerScreen<T> {
public abstract class BaseContainerScreen<T extends AbstractContainerMenu> extends AbstractContainerScreen<T> implements ContainerListener {
protected ResourceLocation bgTexture;
protected int bgImgWidth;
protected int bgImgHeight;

public BaseContainerScreen(T container, Inventory inventory, Component title, ResourceLocation bgTexture) {
this(container, inventory, title, bgTexture, 176, 166, 256, 256);
}

public BaseContainerScreen(T container, Inventory inventory, Component title, ResourceLocation bgTexture, int bgWidth, int bgHeight) {
this(container, inventory, title, bgTexture, bgWidth, bgHeight, 256, 256);
}
Expand All @@ -44,22 +50,43 @@ protected static String fraction(Object number) {
return df.format(number);
}

protected void subInit() {
}

protected void init() {
super.init();
this.subInit();
this.menu.addSlotListener(this);
}

public void removed() {
super.removed();
this.menu.removeSlotListener(this);
}

@Override
public void render(@NotNull GuiGraphics matrix, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(matrix);
super.render(matrix, mouseX, mouseY, partialTicks);
this.renderTooltip(matrix, mouseX, mouseY);
public void render(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
this.renderBackground(pGuiGraphics);
super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
this.renderFg(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
this.renderTooltip(pGuiGraphics, pMouseX, pMouseY);
}

protected void renderDefaultBg(GuiGraphics matrix, float uOffSet, float vOffSet) {
int x = this.getGuiLeft();
int y = this.getGuiTop();
matrix.blit(this.bgTexture, x, y, uOffSet, vOffSet, this.imageWidth, this.imageHeight, this.bgImgWidth, this.bgImgHeight);
protected void renderFg(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
}

@Override
protected void renderBg(@NotNull GuiGraphics pGuiGraphics, float pPartialTick, int pMouseX, int pMouseY) {
pGuiGraphics.blit(this.bgTexture, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight, this.bgImgWidth, this.bgImgHeight);
this.renderBgOthers(pGuiGraphics, this.leftPos, this.topPos);
}

protected abstract void renderBgOthers(GuiGraphics pGuiGraphics, int pX, int pY);


public void dataChanged(@NotNull AbstractContainerMenu pContainerMenu, int pDataSlotIndex, int pValue) {
}

protected void renderDefaultBg(GuiGraphics matrix) {
int x = this.getGuiLeft();
int y = this.getGuiTop();
matrix.blit(this.bgTexture, x, y, 0, 0, this.imageWidth, this.imageHeight, this.bgImgWidth, this.bgImgHeight);
public void slotChanged(@NotNull AbstractContainerMenu pContainerToSend, int pSlotInd, @NotNull ItemStack pStack) {
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package committee.nova.mods.avaritia.api.common.menu;

import net.minecraft.core.BlockPos;
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.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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Description:
* Author: cnlimiter
* Date: 2022/2/19 19:46
* Version: 1.0
*/
public abstract class BaseMenu extends AbstractContainerMenu {
public abstract class BaseMenu<T extends BlockEntity> extends AbstractContainerMenu {

private BlockPos pos;
public final Level level;
public final Player player;

protected BaseMenu(MenuType<?> menu, int id, @Nullable BlockPos pos) {
this(menu, id);
protected BaseMenu(MenuType<?> menu, int id, Inventory playerInventory, @NotNull BlockPos pos) {
this(menu, id, playerInventory);
this.pos = pos;
}

protected BaseMenu(MenuType<?> menu, int id) {
protected BaseMenu(MenuType<?> menu, int id, Inventory playerInventory) {
super(menu, id);
this.player = playerInventory.player;
this.level = playerInventory.player.level();
}

@Override
Expand All @@ -41,4 +48,20 @@ public BlockPos getBlockPos() {
return this.pos;
}


@SuppressWarnings("unchecked")
protected T getTileEntity() {
return (T) level.getBlockEntity(pos);
}

protected void createInventorySlots(Inventory pInventory) {
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 9; ++j) {
this.addSlot(new Slot(pInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
for(int k = 0; k < 9; ++k) {
this.addSlot(new Slot(pInventory, k, 8 + k * 18, 142));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Date: 2022/4/2 14:00
* Version: 1.0
*/
public class BaseTileEntity extends BlockEntity {
public abstract class BaseTileEntity extends BlockEntity {
private boolean isChanged = false;

public BaseTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @CreateTime: 2024/7/13 上午11:32
* @Description:
*/
public class CompressedChestScreen extends BaseContainerScreen<CompressedChestMenu> implements MenuAccess<CompressedChestMenu> {
public class CompressedChestScreen extends BaseContainerScreen<CompressedChestMenu>{
private static final ResourceLocation CONTAINER_BACKGROUND = Static.rl("textures/gui/generic_243.png");

public CompressedChestScreen(CompressedChestMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
Expand All @@ -28,7 +28,7 @@ public CompressedChestScreen(CompressedChestMenu pMenu, Inventory pPlayerInvento
}

@Override
protected void renderBg(@NotNull GuiGraphics pGuiGraphics, float pPartialTick, int pMouseX, int pMouseY) {
renderDefaultBg(pGuiGraphics);
protected void renderBgOthers(GuiGraphics pGuiGraphics, int pX, int pY) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CompressorScreen extends BaseContainerScreen<CompressorMenu> {
private CompressorTile tile;

public CompressorScreen(CompressorMenu container, Inventory inventory, Component title) {
super(container, inventory, title, BACKGROUND, 176, 166, 256, 256);
super(container, inventory, title, BACKGROUND);
}

@Override
Expand All @@ -37,14 +37,14 @@ public void init() {
}

@Override
public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
protected void renderFg(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
int x = this.getGuiLeft();
int y = this.getGuiTop();

super.render(guiGraphics, mouseX, mouseY, partialTicks);
super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);


if (mouseX > x + 63 && mouseX < x + 79 && mouseY > y + 35 && mouseY < y + 51) {
if (pMouseX > x + 63 && pMouseX < x + 79 && pMouseY > y + 35 && pMouseY < y + 51) {
List<Component> tooltip = new ArrayList<>();

if (this.getMaterialCount() < 1) {
Expand All @@ -59,63 +59,34 @@ public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, flo
tooltip.add(text);
}

guiGraphics.renderComponentTooltip(font, tooltip, mouseX, mouseY);
pGuiGraphics.renderComponentTooltip(font, tooltip, pMouseX, pMouseY);
}

// if (mouseX > x + 89 && mouseX < x + 110 && mouseY > y + 35 && mouseY < y + 51) {
// List<Component> tooltip = new ArrayList<>();
//
// if (this.getProgress() < 1) {
// tooltip.add(ModTooltips.PROGRESS_EMPTY.color(ChatFormatting.WHITE).build());
// } else {
//
// var text = new TextComponent(number(this.getProgress()) + " / " + number(this.getTimeRequired()));
//
// tooltip.add(text);
// }
//
// this.renderComponentTooltip(matrix, tooltip, mouseX, mouseY);
// }

// if (mouseX > x + 68 && mouseX < x + 79 && mouseY > y + 28 && mouseY < y + 39) {
// if (this.isEjecting()) {
// this.renderTooltip(matrix, ModTooltips.EJECTING.color(ChatFormatting.WHITE).build(), mouseX, mouseY);
// } else {
// this.renderTooltip(matrix, ModTooltips.EJECT.color(ChatFormatting.WHITE).build(), mouseX, mouseY);
// }
// }

}


@Override
protected void renderLabels(@NotNull GuiGraphics stack, int mouseX, int mouseY) {
var title = this.getTitle().getString();
stack.drawString(font, title, (this.imageWidth / 2 - this.font.width(title) / 2), 6, 4210752, false);
stack.drawString(font, this.playerInventoryTitle, 8, this.imageHeight - 94, 4210752, false);
}

@Override
protected void renderBg(@NotNull GuiGraphics stack, float partialTicks, int mouseX, int mouseY) {
this.renderDefaultBg(stack);

@Override
protected void renderBgOthers(GuiGraphics pGuiGraphics, int pX, int pY) {
int x = this.getGuiLeft();
int y = this.getGuiTop();

if (this.hasRecipe()) {
if (this.getMaterialCount() > 0 && this.getMaterialsRequired() > 0) {
int i2 = this.getMaterialBarScaled(16);
stack.blit(BACKGROUND, x + 63, y + 35, 176, 18, i2 + 1, 16);
pGuiGraphics.blit(BACKGROUND, x + 63, y + 35, 176, 18, i2 + 1, 16);
}

if (this.getProgress() > 0 && this.getMaterialCount() >= this.getMaterialsRequired()) {
int i2 = this.getProgressBarScaled(22);
stack.blit(BACKGROUND, x + 89, y + 35, 176, 0, i2 + 1, 16);
pGuiGraphics.blit(BACKGROUND, x + 89, y + 35, 176, 0, i2 + 1, 16);
}

}


}

private Component getMaterialStackDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package committee.nova.mods.avaritia.client.screen;

import committee.nova.mods.avaritia.Static;
import committee.nova.mods.avaritia.api.client.screen.BaseContainerScreen;
import committee.nova.mods.avaritia.common.menu.ExtremeAnvilMenu;
import committee.nova.mods.avaritia.common.net.C2SRenamePacket;
import committee.nova.mods.avaritia.init.handler.NetworkHandler;
Expand All @@ -23,7 +24,7 @@
* @CreateTime: 2024/12/23 18:39
* @Description:
*/
public class ExtremeAnvilScreen extends ItemCombinerScreen<ExtremeAnvilMenu> {
public class ExtremeAnvilScreen extends BaseContainerScreen<ExtremeAnvilMenu> {
private static final ResourceLocation ANVIL_LOCATION = Static.rl("textures/gui/extreme_anvil_gui.png");
private static final Component TOO_EXPENSIVE_TEXT = Component.translatable("container.repair.expensive");
private EditBox name;
Expand Down Expand Up @@ -130,7 +131,7 @@ public void renderFg(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY
}

@Override
protected void renderErrorIcon(@NotNull GuiGraphics pGuiGraphics, int pX, int pY) {
protected void renderBgOthers(@NotNull GuiGraphics pGuiGraphics, int pX, int pY) {
if ((this.menu.getSlot(0).hasItem() || this.menu.getSlot(1).hasItem()) && !this.menu.getSlot(this.menu.getResultSlot()).hasItem()) {
pGuiGraphics.blit(ANVIL_LOCATION, pX + 99, pY + 47, this.imageWidth, 0, 28, 21);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class NeutronCollectorScreen extends BaseContainerScreen<NeutronCollector
private BaseNeutronCollectorTile tile;

public NeutronCollectorScreen(NeutronCollectorMenu container, Inventory inventory, Component title) {
super(container, inventory, title, BACKGROUND, 176, 166, 256, 256);
super(container, inventory, title, BACKGROUND);
}

@Override
Expand Down Expand Up @@ -76,13 +76,12 @@ protected void renderLabels(@NotNull GuiGraphics stack, int mouseX, int mouseY)
}

@Override
protected void renderBg(@NotNull GuiGraphics stack, float pPartialTick, int pMouseX, int pMouseY) {
protected void renderBgOthers(GuiGraphics pGuiGraphics, int pX, int pY) {
int i = this.getGuiLeft();
int j = this.getGuiTop();
stack.blit(BACKGROUND, i, j, 0.0F, 0.0F, this.imageWidth, this.imageHeight, this.bgImgWidth, this.bgImgHeight);
if (this.getProgress() > 0) {
int i2 = this.getProgressBarScaled(18);
stack.blit(BACKGROUND, i + 99, j + 49 - i2, 176, 18 - i2, 4, i2);
pGuiGraphics.blit(BACKGROUND, i + 99, j + 49 - i2, 176, 18 - i2, 4, i2);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public NeutronRingScreen(NeutronRingMenu pMenu, Inventory pPlayerInventory, Comp
}

@Override
protected void renderBg(@NotNull GuiGraphics guiGraphics, float v, int i, int i1) {
this.renderDefaultBg(guiGraphics);
protected void renderBgOthers(GuiGraphics pGuiGraphics, int pX, int pY) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import committee.nova.mods.avaritia.Static;
import committee.nova.mods.avaritia.api.client.screen.BaseContainerScreen;
import committee.nova.mods.avaritia.common.menu.ModCraftMenu;
import committee.nova.mods.avaritia.common.menu.TierCraftMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -15,23 +15,22 @@
* Date: 2022/4/2 11:40
* Version: 1.0
*/
public class EndCraftScreen extends BaseContainerScreen<ModCraftMenu> {
public class EndCraftScreen extends BaseContainerScreen<TierCraftMenu> {
private static final ResourceLocation BACKGROUND = new ResourceLocation(Static.MOD_ID, "textures/gui/craft/end_crafting_table_gui.png");

public EndCraftScreen(ModCraftMenu container, Inventory inventory, Component title) {
super(container, inventory, title, BACKGROUND, 200, 242, 256, 256);
public EndCraftScreen(TierCraftMenu container, Inventory inventory, Component title) {
super(container, inventory, title, BACKGROUND, 200, 242);
}

@Override
protected void renderLabels(GuiGraphics stack, int mouseX, int mouseY) {
protected void renderLabels(@NotNull GuiGraphics stack, int mouseX, int mouseY) {
var title = this.getTitle().getString();

stack.drawString(font, title, 27, 148, 4210752, false);
//stack.drawString(font, this.playerInventoryTitle, 39, this.imageHeight - 94, 4210752, false);
}

@Override
protected void renderBg(@NotNull GuiGraphics stack, float partialTicks, int mouseX, int mouseY) {
this.renderDefaultBg(stack);
protected void renderBgOthers(GuiGraphics pGuiGraphics, int pX, int pY) {

}
}
Loading

0 comments on commit 6e59016

Please sign in to comment.