Skip to content

Commit

Permalink
refactor inner classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Nov 18, 2024
1 parent 17ca6f2 commit cff95ea
Show file tree
Hide file tree
Showing 21 changed files with 202 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.lothrazar.storagenetwork.block.cable.inputfilter.ScreenCableImportFilter;
import com.lothrazar.storagenetwork.block.cable.linkfilter.ScreenCableFilter;
import com.lothrazar.storagenetwork.block.collection.ScreenCollectionFilter;
import com.lothrazar.storagenetwork.block.inventory.ScreenNetworkInventory;
import com.lothrazar.storagenetwork.block.expand.ScreenNetworkInventoryExpanded;
import com.lothrazar.storagenetwork.block.inventory.ScreenNetworkInventory;
import com.lothrazar.storagenetwork.block.request.ScreenNetworkTable;
import com.lothrazar.storagenetwork.item.remote.ScreenNetworkCraftingRemote;
import com.lothrazar.storagenetwork.item.remote.ScreenNetworkRemote;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public interface IGuiNetwork extends IGuiPrivate {

EnumSortType getSort();

default BlockPos getPos() {
return null;
}
default BlockPos getPos() {
return null;
}

default void syncDataToServer() {
PacketRegistry.INSTANCE.sendToServer(new SettingsSyncMessage(getPos(), getDownwards(), getSort(), isJeiSearchSynced(), getAutoFocus()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.lothrazar.storagenetwork.block;

import java.util.List;
import com.lothrazar.storagenetwork.StorageNetworkMod;
import com.lothrazar.storagenetwork.api.IGuiNetwork;
import com.lothrazar.storagenetwork.gui.NetworkWidget;
import com.lothrazar.storagenetwork.gui.NetworkScreenSize;
import com.lothrazar.storagenetwork.gui.TextboxInteger;
import com.lothrazar.storagenetwork.jei.JeiHooks;
import com.lothrazar.storagenetwork.network.ClearRecipeMessage;
Expand All @@ -15,117 +16,117 @@
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import java.util.List;

public abstract class AbstractNetworkScreen<T extends AbstractContainerMenu> extends AbstractContainerScreen<T> implements IGuiNetwork {
public abstract class AbstractNetworkScreen<T extends AbstractContainerMenu> extends AbstractContainerScreen<T> implements IGuiNetwork {

public AbstractNetworkScreen(T container, Inventory inv, Component name) {
super(container, inv, name);
}
public AbstractNetworkScreen(T container, Inventory inv, Component name) {
super(container, inv, name);
}

@Override
public void setStacks(List<ItemStack> stacks) {
getNetwork().setStacks(stacks);
}
@Override
public void setStacks(List<ItemStack> stacks) {
getNetwork().setStacks(stacks);
}

@Override
public void drawGradient(GuiGraphics ms, int x, int y, int x2, int y2, int u, int v) {
ms.fillGradient(x, y, x2, y2, u, v);
}
@Override
public void drawGradient(GuiGraphics ms, int x, int y, int x2, int y2, int u, int v) {
ms.fillGradient(x, y, x2, y2, u, v);
}

@Override
public boolean charTyped(char typedChar, int keyCode) {
if (getNetwork().charTyped(typedChar, keyCode)) {
return true;
}
return false;
@Override
public boolean charTyped(char typedChar, int keyCode) {
if (getNetwork().charTyped(typedChar, keyCode)) {
return true;
}
return false;
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int b) {
InputConstants.Key mouseKey = InputConstants.getKey(keyCode, scanCode);
if (keyCode == TextboxInteger.KEY_ESC) {
minecraft.player.closeContainer();
return true; // Forge MC-146650: Needs to return true when the key is handled.
}
if (getNetwork().searchBar.isFocused()) {
if (keyCode == TextboxInteger.KEY_BACKSPACE) { // BACKSPACE
getNetwork().syncTextToJei();
}
getNetwork().searchBar.keyPressed(keyCode, scanCode, b);
return true;
}
else if (!getNetwork().stackUnderMouse.isEmpty()) {
try {
JeiHooks.testJeiKeybind(mouseKey, getNetwork().stackUnderMouse);
}
catch (Throwable e) {
StorageNetworkMod.LOGGER.error("Error thrown from JEI API ", e);
}
}
//Regardless of above branch, also check this
if (minecraft.options.keyInventory.isActiveAndMatches(mouseKey)) {
minecraft.player.closeContainer();
return true; // Forge MC-146650: Needs to return true when the key is handled.
}
return super.keyPressed(keyCode, scanCode, b);
@Override
public boolean keyPressed(int keyCode, int scanCode, int b) {
InputConstants.Key mouseKey = InputConstants.getKey(keyCode, scanCode);
if (keyCode == TextboxInteger.KEY_ESC) {
minecraft.player.closeContainer();
return true; // Forge MC-146650: Needs to return true when the key is handled.
}

// used by ItemSlotNetwork and NetworkWidget
@Override
public boolean isInRegion(int x, int y, int width, int height, double mouseX, double mouseY) {
return super.isHovering(x, y, width, height, mouseX, mouseY);
if (getNetwork().searchBar.isFocused()) {
if (keyCode == TextboxInteger.KEY_BACKSPACE) { // BACKSPACE
getNetwork().syncTextToJei();
}
getNetwork().searchBar.keyPressed(keyCode, scanCode, b);
return true;
}
else if (!getNetwork().stackUnderMouse.isEmpty()) {
try {
JeiHooks.testJeiKeybind(mouseKey, getNetwork().stackUnderMouse);
}
catch (Throwable e) {
StorageNetworkMod.LOGGER.error("Error thrown from JEI API ", e);
}
}
//Regardless of above branch, also check this
if (minecraft.options.keyInventory.isActiveAndMatches(mouseKey)) {
minecraft.player.closeContainer();
return true; // Forge MC-146650: Needs to return true when the key is handled.
}
return super.keyPressed(keyCode, scanCode, b);
}

public boolean isScrollable(double x, double y) {
// used by ItemSlotNetwork and NetworkWidget
@Override
public boolean isInRegion(int x, int y, int width, int height, double mouseX, double mouseY) {
return super.isHovering(x, y, width, height, mouseX, mouseY);
}

return isHovering(getNetwork().xNetwork, getNetwork().yNetwork,
this.width, getNetwork().scrollHeight,
x, y);
}
@Override
public boolean mouseScrolled(double x, double y, double mouseButton) {
super.mouseScrolled(x, y, mouseButton);
//<0 going down
// >0 going up
if (isScrollable(x, y) && mouseButton != 0) {
getNetwork().mouseScrolled(mouseButton);
}
return true;
public boolean isScrollable(double x, double y) {
return isHovering(getNetwork().xNetwork, getNetwork().yNetwork,
this.width, getNetwork().scrollHeight,
x, y);
}

@Override
public boolean mouseScrolled(double x, double y, double mouseButton) {
super.mouseScrolled(x, y, mouseButton);
//<0 going down
// >0 going up
if (isScrollable(x, y) && mouseButton != 0) {
getNetwork().mouseScrolled(mouseButton);
}
return true;
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
super.mouseClicked(mouseX, mouseY, mouseButton);
getNetwork().mouseClicked(mouseX, mouseY, mouseButton);
if(getNetwork().getSize() == NetworkWidget.NetworkScreenSize.NORMAL) {
//TODO: this is part of crafting grid, to clear it out. should be its own button class
int rectX = 63;
int rectY = 110;
if (isHovering(rectX, rectY, 7, 7, mouseX, mouseY)) {
PacketRegistry.INSTANCE.sendToServer(new ClearRecipeMessage());
PacketRegistry.INSTANCE.sendToServer(new RequestMessage(0, ItemStack.EMPTY, false, false));
return true;
}
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
super.mouseClicked(mouseX, mouseY, mouseButton);
getNetwork().mouseClicked(mouseX, mouseY, mouseButton);
if (getNetwork().getSize() == NetworkScreenSize.NORMAL) {
//TODO: this is part of crafting grid, to clear it out. should be its own button class
int rectX = 63;
int rectY = 110;
if (isHovering(rectX, rectY, 7, 7, mouseX, mouseY)) {
PacketRegistry.INSTANCE.sendToServer(new ClearRecipeMessage());
PacketRegistry.INSTANCE.sendToServer(new RequestMessage(0, ItemStack.EMPTY, false, false));
return true;
}
}
return true;
}

@Override
public void render(GuiGraphics ms, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(ms);
super.render(ms, mouseX, mouseY, partialTicks);
this.renderTooltip(ms, mouseX, mouseY);
getNetwork().searchBar.render(ms, mouseX, mouseY, partialTicks);
getNetwork().render();
}
@Override
public void renderStackTooltip(GuiGraphics ms, ItemStack stack, int mousex, int mousey) {
ms.renderTooltip(font, stack, mousex, mousey);
}
@Override
public void render(GuiGraphics ms, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(ms);
super.render(ms, mouseX, mouseY, partialTicks);
this.renderTooltip(ms, mouseX, mouseY);
getNetwork().searchBar.render(ms, mouseX, mouseY, partialTicks);
getNetwork().render();
}

@Override
public void renderLabels(GuiGraphics ms, int mouseX, int mouseY) {
getNetwork().drawGuiContainerForegroundLayer(ms, mouseX, mouseY, font);
}
@Override
public void renderStackTooltip(GuiGraphics ms, ItemStack stack, int mousex, int mousey) {
ms.renderTooltip(font, stack, mousex, mousey);
}

@Override
public void renderLabels(GuiGraphics ms, int mouseX, int mouseY) {
getNetwork().drawGuiContainerForegroundLayer(ms, mouseX, mouseY, font);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,40 @@

public abstract class EntityBlockConnectable extends EntityBlockFlib {

protected boolean hasGui = true;

public EntityBlockConnectable() {
this(Block.Properties.of().strength(0.5F).sound(SoundType.STONE));
}

public EntityBlockConnectable(BlockBehaviour.Properties p) {
super(p);
}

@Override
public RenderShape getRenderShape(BlockState bs) {
return RenderShape.MODEL;
}


@Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult result) {
if (!world.isClientSide && hasGui) {
if( world.getBlockEntity(pos) instanceof TileConnectable tile) {
if (tile.getMain() == null || tile.getMain().getBlockPos() == null) {
return InteractionResult.PASS;
}
//sync
ServerPlayer sp = (ServerPlayer) player;
PacketRegistry.INSTANCE.sendTo(new SortClientMessage(pos, tile.isDownwards(), tile.getSort()), sp.connection.connection, NetworkDirection.PLAY_TO_CLIENT);
//end sync
if (tile instanceof MenuProvider) {
NetworkHooks.openScreen((ServerPlayer) player, (MenuProvider) tile, tile.getBlockPos());
}
else {
throw new IllegalStateException("Our named container provider is missing!");
}
protected boolean hasGui = true;

public EntityBlockConnectable() {
this(Block.Properties.of().strength(0.5F).sound(SoundType.STONE));
}

public EntityBlockConnectable(BlockBehaviour.Properties p) {
super(p);
}

@Override
public RenderShape getRenderShape(BlockState bs) {
return RenderShape.MODEL;
}

@Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult result) {
if (!world.isClientSide && hasGui) {
if (world.getBlockEntity(pos) instanceof TileConnectable tile) {
if (tile.getMain() == null || tile.getMain().getBlockPos() == null) {
return InteractionResult.PASS;
}
//sync
ServerPlayer sp = (ServerPlayer) player;
PacketRegistry.INSTANCE.sendTo(new SortClientMessage(pos, tile.isDownwards(), tile.getSort()), sp.connection.connection, NetworkDirection.PLAY_TO_CLIENT);
//end sync
if (tile instanceof MenuProvider) {
NetworkHooks.openScreen((ServerPlayer) player, (MenuProvider) tile, tile.getBlockPos());
}
else {
throw new IllegalStateException("Our named container provider is missing!");
}
return InteractionResult.SUCCESS;
}
}
return InteractionResult.SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.lothrazar.storagenetwork.block;


public enum TableSize {
NORMAL, EXPANDED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public TileConnectable(BlockEntityType<?> tileEntityTypeIn, BlockPos pos, BlockS
connectable = new CapabilityConnectable();
}

public EnumSortType getSort() {
public EnumSortType getSort() {
return EnumSortType.NAME; //unused by some blocks
}

public boolean isDownwards() {
return false; //unused by some blocks
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ public BlockInventoryExpanded() {
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new TileInventoryExpanded(pos, state);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.lothrazar.storagenetwork.block.expand;

import com.lothrazar.storagenetwork.block.inventory.ContainerNetworkInventory;
import com.lothrazar.storagenetwork.block.inventory.TileInventory;
import com.lothrazar.storagenetwork.block.main.TileMain;
import com.lothrazar.storagenetwork.gui.ContainerNetwork;
import com.lothrazar.storagenetwork.registry.SsnRegistry;
Expand All @@ -12,22 +10,23 @@
import net.minecraft.world.level.Level;

public class ContainerNetworkInventoryExpanded extends ContainerNetwork {

TileInventoryExpanded tile;
public ContainerNetworkInventoryExpanded(int windowId, Level world, BlockPos pos, Inventory playerInv, Player player) {

public ContainerNetworkInventoryExpanded(int windowId, Level world, BlockPos pos, Inventory playerInv, Player player) {
this(SsnRegistry.Menus.INVENTORY_EXPANDED.get(), windowId, world, pos, playerInv, player);

// super(SsnRegistry.Menus.INVENTORY_EXPANDED.get(), windowId, world, pos, playerInv, player);

}

public ContainerNetworkInventoryExpanded(MenuType<?> menuType, int windowId, Level world, BlockPos pos, Inventory playerInv, Player player) {
super(menuType, windowId);
tile = (TileInventoryExpanded) world.getBlockEntity(pos);
this.yPlayer = 174 +128; //////////////////
this.yPlayer = 174 + 128; //////////////////
this.playerInv = playerInv;
bindPlayerInvo(this.playerInv);
bindHotbar();
}

@Override
public boolean isCrafting() {
return false;
Expand Down
Loading

0 comments on commit cff95ea

Please sign in to comment.