diff --git a/src/main/java/slimeknights/mantle/client/screen/ModuleScreen.java b/src/main/java/slimeknights/mantle/client/screen/ModuleScreen.java index 1579aaf..65b5ba8 100644 --- a/src/main/java/slimeknights/mantle/client/screen/ModuleScreen.java +++ b/src/main/java/slimeknights/mantle/client/screen/ModuleScreen.java @@ -52,15 +52,13 @@ public abstract class ModuleScreen extends ContainerScreen { public void updatePosition(int parentX, int parentY, int parentSizeX, int parentSizeY) { if (this.right) { this.guiLeft = parentX + parentSizeX; - } - else { + } else { this.guiLeft = parentX - this.xSize; } if (this.bottom) { this.guiTop = parentY + parentSizeY - this.ySize; - } - else { + } else { this.guiTop = parentY; } @@ -73,8 +71,7 @@ public abstract class ModuleScreen extends ContainerScreen { } public boolean isMouseInModule(int mouseX, int mouseY) { - return mouseX >= this.guiLeft && mouseX < this.guiRight() && - mouseY >= this.guiTop && mouseY < this.guiBottom(); + return mouseX >= this.guiLeft && mouseX < this.guiRight() && mouseY >= this.guiTop && mouseY < this.guiBottom(); } public boolean isMouseOverFullSlot(double mouseX, double mouseY) { @@ -86,18 +83,6 @@ public abstract class ModuleScreen extends ContainerScreen { return false; } - /* - public void updateDragged(boolean dragSplitting, Set draggedSlots) { - this.dragSplitting = dragSplitting; - this.dragSplittingSlots.clear(); - for(Object o : draggedSlots) { - if(o instanceof SlotWrapper) - this.dragSplittingSlots.add(((SlotWrapper) o).parent); - else - this.dragSplittingSlots.add(o); - } - } - */ public void handleDrawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { this.drawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY); } @@ -133,4 +118,13 @@ public abstract class ModuleScreen extends ContainerScreen { return false; } + /** + * Custom mouse scrolled handling. + * + * @return True to prevent the main container handling the mouseclick + */ + public boolean handleMouseScrolled(double mouseX, double mouseY, double delta) { + return false; + } + } diff --git a/src/main/java/slimeknights/mantle/client/screen/MultiModuleScreen.java b/src/main/java/slimeknights/mantle/client/screen/MultiModuleScreen.java index f69b755..2d49ae8 100644 --- a/src/main/java/slimeknights/mantle/client/screen/MultiModuleScreen.java +++ b/src/main/java/slimeknights/mantle/client/screen/MultiModuleScreen.java @@ -1,7 +1,6 @@ package slimeknights.mantle.client.screen; import com.google.common.collect.Lists; -import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screen.inventory.ContainerScreen; @@ -11,14 +10,14 @@ import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.Slot; import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.ITextComponent; -import slimeknights.mantle.inventory.MultiModuleContainer; import slimeknights.mantle.inventory.WrapperSlot; +import slimeknights.mantle.inventory.MultiModuleContainer; import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; -public class MultiModuleScreen extends ContainerScreen { +public class MultiModuleScreen> extends ContainerScreen { protected List modules = Lists.newArrayList(); @@ -27,11 +26,12 @@ public class MultiModuleScreen extends Container public int realWidth; public int realHeight; - public MultiModuleScreen(T container, PlayerInventory playerInventory, ITextComponent title) { + public MultiModuleScreen(CONTAINER container, PlayerInventory playerInventory, ITextComponent title) { super(container, playerInventory, title); this.realWidth = -1; this.realHeight = -1; + this.passEvents = true; } protected void addModule(ModuleScreen module) { @@ -53,6 +53,7 @@ public class MultiModuleScreen extends Container this.xSize = this.realWidth; this.ySize = this.realHeight; } + super.init(); this.cornerX = this.guiLeft; @@ -63,10 +64,6 @@ public class MultiModuleScreen extends Container for (ModuleScreen module : this.modules) { this.updateSubmodule(module); } - - //this.guiLeft = this.guiTop = 0; - //this.xSize = width; - //this.ySize = height; } @Override @@ -165,13 +162,16 @@ public class MultiModuleScreen extends Container this.xSize += this.guiLeft - module.guiLeft; this.guiLeft = module.guiLeft; } + if (module.guiTop < this.guiTop) { this.ySize += this.guiTop - module.guiTop; this.guiTop = module.guiTop; } + if (module.guiRight() > this.guiLeft + this.xSize) { this.xSize = module.guiRight() - this.guiLeft; } + if (module.guiBottom() > this.guiTop + this.ySize) { this.ySize = module.guiBottom() - this.guiTop; } @@ -187,6 +187,7 @@ public class MultiModuleScreen extends Container if (slotIn instanceof WrapperSlot) { slot = ((WrapperSlot) slotIn).parent; } + if (!module.shouldDrawSlot(slot)) { return; } @@ -212,6 +213,7 @@ public class MultiModuleScreen extends Container if (slotIn instanceof WrapperSlot) { slot = ((WrapperSlot) slotIn).parent; } + if (!module.shouldDrawSlot(slot)) { return false; } @@ -223,17 +225,20 @@ public class MultiModuleScreen extends Container @Override public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { ModuleScreen module = this.getModuleForPoint(mouseX, mouseY); + if (module != null) { if (module.handleMouseClicked(mouseX, mouseY, mouseButton)) { return false; } } + return super.mouseClicked(mouseX, mouseY, mouseButton); } @Override public boolean mouseDragged(double mouseX, double mouseY, int clickedMouseButton, double timeSinceLastClick, double unkowwn) { ModuleScreen module = this.getModuleForPoint(mouseX, mouseY); + if (module != null) { if (module.handleMouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick)) { return false; @@ -243,9 +248,23 @@ public class MultiModuleScreen extends Container return super.mouseDragged(mouseX, mouseY, clickedMouseButton, timeSinceLastClick, unkowwn); } + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double delta) { + ModuleScreen module = this.getModuleForPoint(mouseX, mouseY); + + if (module != null) { + if (module.handleMouseScrolled(mouseX, mouseY, delta)) { + return false; + } + } + + return super.mouseScrolled(mouseX, mouseY, delta); + } + @Override public boolean mouseReleased(double mouseX, double mouseY, int state) { ModuleScreen module = this.getModuleForPoint(mouseX, mouseY); + if (module != null) { if (module.handleMouseReleased(mouseX, mouseY, state)) { return false; @@ -257,8 +276,7 @@ public class MultiModuleScreen extends Container protected ModuleScreen getModuleForPoint(double x, double y) { for (ModuleScreen module : this.modules) { - if (this.isPointInRegion(module.guiLeft, module.guiTop, module.guiRight(), module.guiBottom(), - x + this.cornerX, y + this.cornerY)) { + if (this.isPointInRegion(module.guiLeft, module.guiTop, module.guiRight(), module.guiBottom(), x + this.cornerX, y + this.cornerY)) { return module; } } @@ -282,8 +300,7 @@ public class MultiModuleScreen extends Container @Nonnull @Override - public T getContainer() { + public CONTAINER getContainer() { return this.container; } - } diff --git a/src/main/java/slimeknights/mantle/client/screen/SliderWidget.java b/src/main/java/slimeknights/mantle/client/screen/SliderWidget.java index eec2ace..03d1534 100644 --- a/src/main/java/slimeknights/mantle/client/screen/SliderWidget.java +++ b/src/main/java/slimeknights/mantle/client/screen/SliderWidget.java @@ -1,6 +1,5 @@ package slimeknights.mantle.client.screen; -import net.minecraft.client.Minecraft; import net.minecraft.util.math.MathHelper; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -35,6 +34,7 @@ public class SliderWidget extends Widget { private int clickX; private int clickY; private boolean clickedBar; // if the bar has already been clicked and not released + private boolean leftMouseDown = false; public SliderWidget(ElementScreen slider, ElementScreen sliderHighlighted, ElementScreen sliderDisabled, ElementScreen slideBarTop, ElementScreen slideBarBottom, ScalableElementScreen slideBar) { this.slider = slider; @@ -59,12 +59,16 @@ public class SliderWidget extends Widget { this.hidden = false; } - /** Sets the height of the whole slider and slidebar */ + /** + * Sets the height of the whole slider and slidebar + */ public void setSize(int height) { this.height = height; } - /** specifies the values that the slider represents */ + /** + * specifies the values that the slider represents + */ public void setSliderParameters(int min, int max, int stepsize) { this.minValue = min; this.maxValue = max; @@ -119,7 +123,7 @@ public class SliderWidget extends Widget { return; } - // slidebar background + // slide bar background this.slideBarTop.draw(this.xPos, this.yPos); this.slideBar.drawScaledY(this.xPos, this.yPos + this.slideBarTop.h, this.getUsableSlidebarHeight()); this.slideBarBottom.draw(this.xPos, this.yPos + this.height - this.slideBarBottom.h); @@ -131,15 +135,12 @@ public class SliderWidget extends Widget { if (this.enabled) { if (this.isScrolling) { this.sliderDisabled.draw(x, y); - } - else if (this.isHighlighted) { + } else if (this.isHighlighted) { this.sliderHighlighted.draw(x, y); - } - else { + } else { this.slider.draw(x, y); } - } - else { + } else { this.sliderDisabled.draw(x, y); } } @@ -149,19 +150,17 @@ public class SliderWidget extends Widget { return; } - boolean mouseDown = Minecraft.getInstance().mouseHelper.isLeftDown(); // left mouse button - // relative position inside the widget int x = mouseX - this.xPos; int y = mouseY - this.yPos; // reset click data - if (!mouseDown && this.clickedBar) { + if (!this.leftMouseDown && this.clickedBar) { this.clickedBar = false; } // button not pressed and scrolling -> stop scrolling - if (!mouseDown && this.isScrolling) { + if (!this.leftMouseDown && this.isScrolling) { this.isScrolling = false; } // button pressed and scrolling -> update position of slider @@ -173,56 +172,64 @@ public class SliderWidget extends Widget { if (val < (float) this.increment / 2f) { // < 1/2 increment this.setSliderValue(this.minValue); - } - else if (val > this.maxValue - ((float) this.increment / 2f)) { + } else if (val > this.maxValue - ((float) this.increment / 2f)) { // > max-1/2 increment this.setSliderValue(this.maxValue); - } - else { + } else { // in between this.setSliderValue((int) (this.minValue + (float) this.increment * Math.round(val))); } } // not scrolling yet but possibly inside the slider else if (x >= 0 && y >= this.getSliderTop() && - x - this.sliderOffset <= this.slider.w && y <= this.getSliderTop() + this.slider.h) { + x - this.sliderOffset <= this.slider.w && y <= this.getSliderTop() + this.slider.h) { this.isHighlighted = true; - if (mouseDown) { + if (this.leftMouseDown) { this.isScrolling = true; this.clickX = x - this.sliderOffset; this.clickY = y - this.getSliderTop(); } } // not on the slider but clicked on the bar - else if (mouseDown && !this.clickedBar && - x >= 0 && y >= 0 && - x <= this.slideBar.w && y <= this.height) { + else if (this.leftMouseDown && !this.clickedBar && + x >= 0 && y >= 0 && + x <= this.slideBar.w && y <= this.height) { if (y < this.getSliderTop()) { this.decrement(); - } - else { + } else { this.increment(); } this.clickedBar = true; - } - else { + } else { this.isHighlighted = false; } } + @Override + public void handleMouseClicked(int mouseX, int mouseY, int mouseButton) { + if (mouseButton == 0) { + this.leftMouseDown = true; + } + } + + @Override + public void handleMouseReleased() { + this.leftMouseDown = false; + } + // Call this via Screen.mouseScrolled on your screen if you wish to use scroll data. - public boolean mouseScrolled(double mouseX, double mouseY, double scrollData, boolean useMouseWheel) { + public boolean mouseScrolled(double scrollData, boolean useMouseWheel) { if (useMouseWheel) { if (scrollData > 0.0) { this.decrement(); return true; - } - else if (scrollData < 0.0) { + } else if (scrollData < 0.0) { this.increment(); return true; } } + return true; } @@ -239,8 +246,7 @@ public class SliderWidget extends Widget { public int setSliderValue(int val) { if (val > this.maxValue) { val = this.maxValue; - } - else if (val < this.minValue) { + } else if (val < this.minValue) { val = this.minValue; } diff --git a/src/main/java/slimeknights/mantle/client/screen/TabsWidget.java b/src/main/java/slimeknights/mantle/client/screen/TabsWidget.java index 31f6223..af8dfeb 100644 --- a/src/main/java/slimeknights/mantle/client/screen/TabsWidget.java +++ b/src/main/java/slimeknights/mantle/client/screen/TabsWidget.java @@ -15,8 +15,7 @@ import java.util.List; @OnlyIn(Dist.CLIENT) public class TabsWidget extends Widget { - private static final ResourceLocation - creativeInventoryTabs = new ResourceLocation("textures/gui/container/creative_inventory/tabs.png"); + private static final ResourceLocation creativeInventoryTabs = new ResourceLocation("textures/gui/container/creative_inventory/tabs.png"); private final ElementScreen[] tabActive = new ElementScreen[3]; private final ElementScreen[] tab = new ElementScreen[3]; @@ -34,6 +33,7 @@ public class TabsWidget extends Widget { private final MultiModuleScreen parent; private boolean clicked = false; + private boolean leftMouseDown = false; public TabsWidget(MultiModuleScreen parent, ElementScreen tabLeft, ElementScreen tabCenter, ElementScreen tabRight, ElementScreen activeLeft, ElementScreen activeCenter, ElementScreen activeRight) { this.parent = parent; @@ -57,24 +57,37 @@ public class TabsWidget extends Widget { this.icons.clear(); } - public void update(int mouseX, int mouseY) { - boolean mouseDown = Minecraft.getInstance().mouseHelper.isLeftDown(); // left mouse button + @Override + public void handleMouseClicked(int mouseX, int mouseY, int mouseButton) { + if (mouseButton == 0) { + this.leftMouseDown = true; + } + } + @Override + public void handleMouseReleased() { + this.leftMouseDown = false; + } + + public void update(int mouseX, int mouseY) { // did we click on a tab? mouseX -= this.xPos; mouseY -= this.yPos; // update highlighted this.highlighted = -1; + if (mouseY >= 0 && mouseY <= this.tab[1].h) { // which one did we click? int x = 0; + for (int i = 0; i < this.icons.size(); i++) { // clicking on spacing has no effect if (mouseX >= x && mouseX < x + this.tab[1].w) { this.highlighted = i; break; } + x += this.tab[1].w; x += this.spacing; } @@ -83,17 +96,13 @@ public class TabsWidget extends Widget { // already clicked if (this.clicked) { // still clicking - if (mouseDown) { - return; - } - // release click - else { + if (!this.leftMouseDown) { this.clicked = false; - return; } + return; } // new click - else if (mouseDown) { + else if (this.leftMouseDown) { this.clicked = true; } // no click - do nothing @@ -101,7 +110,7 @@ public class TabsWidget extends Widget { return; } - // was new click, select highlighted + // was a new click, select highlighted if (this.highlighted > -1) { this.selected = this.highlighted; } @@ -120,19 +129,16 @@ public class TabsWidget extends Widget { ElementScreen[] toDraw; if (i == this.selected) { toDraw = this.tabActive; - } - else { + } else { toDraw = this.tab; } ElementScreen actualTab; if (i == 0 && x == this.parent.cornerX) { actualTab = toDraw[0]; - } - else if (x == this.parent.cornerX + this.parent.width) { + } else if (x == this.parent.cornerX + this.parent.width) { actualTab = toDraw[2]; - } - else { + } else { actualTab = toDraw[1]; } @@ -146,21 +152,17 @@ public class TabsWidget extends Widget { if (icon != null) { this.drawItemStack(icon, x + (actualTab.w - 16) / 2, y + (actualTab.h - 16) / 2); RenderHelper.disableStandardItemLighting(); - //RenderHelper.enableStandardItemLighting(); } } } - // guiContainer.drawItemStack + // ContainerScreen.drawItemStack private void drawItemStack(ItemStack stack, int x, int y) { ItemRenderer itemRender = Minecraft.getInstance().getItemRenderer(); RenderSystem.translatef(0.0F, 0.0F, 32.0F); - itemRender.zLevel = 200; - //FontRenderer font = stack.getItem().getFontRenderer(stack); + itemRender.zLevel = 200.0F; itemRender.renderItemAndEffectIntoGUI(stack, x, y); - //this.itemRender.renderItemOverlayIntoGUI(font, stack, x, y - (this.draggedStack == null ? 0 : 8), altText); - //this.zLevel = 0.0F; itemRender.zLevel = 0.0F; } } diff --git a/src/main/java/slimeknights/mantle/client/screen/Widget.java b/src/main/java/slimeknights/mantle/client/screen/Widget.java index 61ece17..3d4e1f7 100644 --- a/src/main/java/slimeknights/mantle/client/screen/Widget.java +++ b/src/main/java/slimeknights/mantle/client/screen/Widget.java @@ -13,7 +13,9 @@ public abstract class Widget { public abstract void draw(); - /** Where the part will be drawn. Upper left corner. */ + /** + * Where the part will be drawn. Upper left corner. + */ public void setPosition(int x, int y) { this.xPos = x; this.yPos = y; @@ -23,5 +25,13 @@ public abstract class Widget { this.width = width; this.height = height; } + + public void handleMouseClicked(int mouseX, int mouseY, int mouseButton) { + + } + + public void handleMouseReleased() { + + } } diff --git a/src/main/java/slimeknights/mantle/inventory/BaseContainer.java b/src/main/java/slimeknights/mantle/inventory/BaseContainer.java index a5d2ad0..75fea8e 100644 --- a/src/main/java/slimeknights/mantle/inventory/BaseContainer.java +++ b/src/main/java/slimeknights/mantle/inventory/BaseContainer.java @@ -1,8 +1,6 @@ package slimeknights.mantle.inventory; -import com.google.common.collect.Lists; -import net.minecraft.block.Block; -import net.minecraft.block.Blocks; +import net.minecraft.client.Minecraft; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.ServerPlayerEntity; @@ -10,63 +8,52 @@ import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.ContainerType; import net.minecraft.inventory.container.Slot; import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketBuffer; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.Direction; import net.minecraft.util.NonNullList; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.server.ServerWorld; import net.minecraft.world.World; -import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.items.CapabilityItemHandler; -import net.minecraftforge.items.IItemHandler; -import net.minecraftforge.items.wrapper.EmptyHandler; +import net.minecraft.world.server.ServerWorld; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.fml.DistExecutor; import slimeknights.mantle.util.SlimeknightException; +import slimeknights.mantle.util.TileEntityUtil; import javax.annotation.Nonnull; -import java.util.List; +import javax.annotation.Nullable; -/** Same as Container but provides some extra functionality to simplify things */ -public abstract class BaseContainer extends Container { +public class BaseContainer extends Container { - protected double maxDist = 8 * 8; // 8 blocks - protected T tile; - protected final Block originalBlock; // used to check if the block we interacted with got broken - protected final BlockPos pos; - protected final World world; - protected final LazyOptional itemHandler; + public static double MAX_DISTANCE = 64; + public static int BASE_Y_OFFSET = 84; - public List subContainers = Lists.newArrayList(); + @Nullable + protected final TILE tile; - public BaseContainer(ContainerType containerType, int windowId, T tile) { - this(containerType, windowId, tile, null); + @Nullable + protected final PlayerInventory inv; + + protected BaseContainer(ContainerType type, int id, @Nullable PlayerInventory inv, @Nullable TILE tile) { + super(type, id); + this.inv = inv; + this.tile = tile; } - public BaseContainer(ContainerType containerType, int windowId, T tile, Direction invDir) { - super(containerType, windowId); - this.tile = tile; - - if (tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, invDir).isPresent()) { - this.itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, invDir); - } - else { - this.itemHandler = net.minecraftforge.common.util.LazyOptional.of(() -> new EmptyHandler()); - } - - this.world = tile.getWorld(); - this.pos = tile.getPos(); - this.originalBlock = this.world.getBlockState(this.pos).getBlock(); + public TILE getTileEntity() { + return this.tile; } public void syncOnOpen(ServerPlayerEntity playerOpened) { // find another player that already has the gui for this tile open ServerWorld server = playerOpened.getServerWorld(); + for (PlayerEntity player : server.getPlayers()) { if (player == playerOpened) { continue; } + if (player.openContainer instanceof BaseContainer) { - if (this.sameGui((BaseContainer) player.openContainer)) { - this.syncWithOtherContainer((BaseContainer) player.openContainer, playerOpened); + if (this.sameGui((BaseContainer) player.openContainer)) { + this.syncWithOtherContainer((BaseContainer) player.openContainer, playerOpened); return; } } @@ -76,19 +63,11 @@ public abstract class BaseContainer extends Container { this.syncNewContainer(playerOpened); } - public T getTile() { - return this.tile; - } - - public IItemHandler getItemHandler() { - return this.itemHandler.orElse(new EmptyHandler()); - } - /** * Called when the container is opened and another player already has a container for this tile open * Sync to the same state here. */ - protected void syncWithOtherContainer(BaseContainer otherContainer, ServerPlayerEntity player) { + protected void syncWithOtherContainer(BaseContainer otherContainer, ServerPlayerEntity player) { } /** @@ -99,77 +78,79 @@ public abstract class BaseContainer extends Container { } public boolean sameGui(BaseContainer otherContainer) { + if (this.tile == null) { + return false; + } + return this.tile == otherContainer.tile; } @Override public boolean canInteractWith(@Nonnull PlayerEntity playerIn) { - Block block = this.world.getBlockState(this.pos).getBlock(); - // does the block we interacted with still exist? - if (block == Blocks.AIR || block != this.originalBlock) { - return false; + if (this.tile == null) { + return true; } - // too far away from block? - return playerIn.getDistanceSq((double) this.pos.getX() + 0.5d, - (double) this.pos.getY() + 0.5d, - (double) this.pos.getZ() + 0.5d) <= this.maxDist; + if (!tile.isRemoved()) { + //prevent Containers from remaining valid after the chunk has unloaded; + World world = tile.getWorld(); + + if (world == null) { + return false; + } + + return world.isBlockPresent(tile.getPos()); + } + + return false; } @Nonnull @Override - @SuppressWarnings("unchecked") public NonNullList getInventory() { return super.getInventory(); } - /*public String getInventoryDisplayName() { - INameable nameable = null; - if (this.itemHandler.orElse(new EmptyHandler()) instanceof InvWrapper) { - nameable = ((InvWrapper) this.itemHandler.orElse(new EmptyHandler())).getInv(); - // if the inventory doesn't have a name fall back to checking the tileentity - if (nameable.getDisplayName() == null) { - nameable = null; - } + /* + * Call this to add the player's inventory to the GUI. + */ + protected void addInventorySlots() { + if (this.inv != null) { + this.addInventorySlots(this.inv); } - if(nameable == null && this.tile instanceof INameable) { - nameable = (INameable) this.tile; - } - if (nameable != null) { - ITextComponent textName = nameable.getDisplayName(); - return textName != null ? textName.getFormattedText() : nameable.getName().toString(); - } - return null; - }*/ - - // standard yOffset calculation for chestlike inventories: - // yOffset = (numRows - 4) * 18; (the -4 because of the 3 rows of inventory + 1 row of hotbar) + } protected int playerInventoryStart = -1; - /** - * Draws the player inventory starting at the given position - * - * @param playerInventory The players inventory - * @param xCorner Default Value: 8 - * @param yCorner Default Value: (rows - 4) * 18 + 103 + /* + * Override this to set the X offset for the inventory slots. */ - protected void addPlayerInventory(PlayerInventory playerInventory, int xCorner, int yCorner) { - int index = 9; + protected int getInventoryXOffset() { + return 8; + } + + /* + * Override this to set the Y offset for the inventory slots. + */ + protected int getInventoryYOffset() { + return BASE_Y_OFFSET; + } + + protected void addInventorySlots(@Nonnull PlayerInventory inv) { + int yOffset = this.getInventoryYOffset(); + int xOffset = this.getInventoryXOffset(); int start = this.inventorySlots.size(); - for (int row = 0; row < 3; row++) { - for (int col = 0; col < 9; col++) { - this.addSlot(new Slot(playerInventory, index, xCorner + col * 18, yCorner + row * 18)); - index++; + for (int slotY = 0; slotY < 3; slotY++) { + for (int slotX = 0; slotX < 9; slotX++) { + addSlot(new Slot(inv, slotX + slotY * 9 + 9, xOffset + slotX * 18, yOffset + slotY * 18)); } } - index = 0; - for (int col = 0; col < 9; col++) { - this.addSlot(new Slot(playerInventory, index, xCorner + col * 18, yCorner + 58)); - index++; + yOffset += 58; + for (int slotY = 0; slotY < 9; slotY++) { + addSlot(new Slot(inv, slotY, xOffset + slotY * 18, yOffset)); } this.playerInventoryStart = start; @@ -216,8 +197,7 @@ public abstract class BaseContainer extends Container { if (itemstack1.isEmpty()) { slot.putStack(ItemStack.EMPTY); - } - else { + } else { slot.onSlotChanged(); } } @@ -256,10 +236,7 @@ public abstract class BaseContainer extends Container { slot = this.inventorySlots.get(k); itemstack1 = slot.getStack(); - if (!itemstack1.isEmpty() - && itemstack1.getItem() == stack.getItem() - && ItemStack.areItemStackTagsEqual(stack, itemstack1) - && this.canMergeSlot(stack, slot)) { + if (!itemstack1.isEmpty() && itemstack1.getItem() == stack.getItem() && ItemStack.areItemStackTagsEqual(stack, itemstack1) && this.canMergeSlot(stack, slot)) { int l = itemstack1.getCount() + stack.getCount(); int limit = Math.min(stack.getMaxStackSize(), slot.getItemStackLimit(stack)); @@ -268,8 +245,7 @@ public abstract class BaseContainer extends Container { itemstack1.setCount(l); slot.onSlotChanged(); flag1 = true; - } - else if (itemstack1.getCount() < limit) { + } else if (itemstack1.getCount() < limit) { stack.shrink(limit - itemstack1.getCount()); itemstack1.setCount(limit); slot.onSlotChanged(); @@ -279,8 +255,7 @@ public abstract class BaseContainer extends Container { if (useEndIndex) { --k; - } - else { + } else { ++k; } } @@ -300,8 +275,7 @@ public abstract class BaseContainer extends Container { if (useEndIndex) { k = endIndex - 1; - } - else { + } else { k = startIndex; } @@ -309,17 +283,18 @@ public abstract class BaseContainer extends Container { Slot slot = this.inventorySlots.get(k); ItemStack itemstack1 = slot.getStack(); - if (itemstack1.isEmpty() && slot.isItemValid(stack) && this.canMergeSlot(stack, slot)) // Forge: Make sure to respect isItemValid in the slot. - { + // Forge: Make sure to respect isItemValid in the slot. + if (itemstack1.isEmpty() && slot.isItemValid(stack) && this.canMergeSlot(stack, slot)) { int limit = slot.getItemStackLimit(stack); ItemStack stack2 = stack.copy(); + if (stack2.getCount() > limit) { stack2.setCount(limit); stack.shrink(limit); - } - else { + } else { stack.setCount(0); } + slot.putStack(stack2); slot.onSlotChanged(); flag1 = true; @@ -331,8 +306,7 @@ public abstract class BaseContainer extends Container { if (useEndIndex) { --k; - } - else { + } else { ++k; } } @@ -340,4 +314,11 @@ public abstract class BaseContainer extends Container { return flag1; } + public static TILE getTileEntityFromBuf(PacketBuffer buf, Class type) { + if (buf == null) { + return null; + } + + return DistExecutor.callWhenOn(Dist.CLIENT, () -> () -> TileEntityUtil.getTileEntity(type, Minecraft.getInstance().world, buf.readBlockPos())); + } } diff --git a/src/main/java/slimeknights/mantle/inventory/MultiModuleContainer.java b/src/main/java/slimeknights/mantle/inventory/MultiModuleContainer.java index 7728b52..cafdf9b 100644 --- a/src/main/java/slimeknights/mantle/inventory/MultiModuleContainer.java +++ b/src/main/java/slimeknights/mantle/inventory/MultiModuleContainer.java @@ -4,6 +4,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.container.ClickType; import net.minecraft.inventory.container.Container; @@ -16,12 +17,13 @@ import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.tuple.Pair; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; -public class MultiModuleContainer extends BaseContainer { +public class MultiModuleContainer extends BaseContainer { public List subContainers = Lists.newArrayList(); @@ -31,8 +33,8 @@ public class MultiModuleContainer extends Bas protected int subContainerSlotStart = -1; protected Set shiftClickContainers = Sets.newHashSet(); - public MultiModuleContainer(ContainerType containerType, int windowId, T tile) { - super(containerType, windowId, tile); + public MultiModuleContainer(ContainerType containerType, int id, @Nullable PlayerInventory inv, TILE tile) { + super(containerType, id, inv, tile); } /** @@ -59,17 +61,18 @@ public class MultiModuleContainer extends Bas this.subContainerSlotRanges.put(subcontainer, Pair.of(begin, end)); } - public TC getSubContainer(Class clazz) { + public CONTAINER getSubContainer(Class clazz) { return this.getSubContainer(clazz, 0); } - public TC getSubContainer(Class clazz, int index) { + public CONTAINER getSubContainer(Class clazz, int index) { for (Container sub : this.subContainers) { if (clazz.isAssignableFrom(sub.getClass())) { index--; } + if (index < 0) { - return (TC) sub; + return (CONTAINER) sub; } } @@ -210,8 +213,7 @@ public class MultiModuleContainer extends Bas return false; } - return this.playerInventoryStart > 0 && !this - .mergeItemStack(itemstack, this.playerInventoryStart, this.inventorySlots.size(), true); + return this.playerInventoryStart > 0 && !this.mergeItemStack(itemstack, this.playerInventoryStart, this.inventorySlots.size(), true); } protected boolean moveToAnyContainer(@Nonnull ItemStack itemstack, Collection containers) { @@ -252,20 +254,35 @@ public class MultiModuleContainer extends Bas return !this.mergeItemStackRefill(itemstack, range.getLeft(), range.getRight(), false); } - /** Searches for a sidechest to display in the UI */ + /** + * Searches for a sidechest to display in the UI + */ public TE detectTE(Class clazz) { - return ObjectUtils.firstNonNull(this.detectChest(this.pos.north(), clazz), - this.detectChest(this.pos.east(), clazz), - this.detectChest(this.pos.south(), clazz), - this.detectChest(this.pos.west(), clazz)); + if (this.tile == null) { + return null; + } else { + return ObjectUtils.firstNonNull(this.detectChest(this.tile.getPos().north(), clazz), + this.detectChest(this.tile.getPos().east(), clazz), + this.detectChest(this.tile.getPos().south(), clazz), + this.detectChest(this.tile.getPos().west(), clazz)); + } } private TE detectChest(BlockPos pos, Class clazz) { - TileEntity te = this.world.getTileEntity(pos); + if (this.tile == null) { + return null; + } else { + if (this.tile.getWorld() == null) { + return null; + } else { + TileEntity te = this.tile.getWorld().getTileEntity(pos); - if (te != null && clazz.isAssignableFrom(te.getClass())) { - return (TE) te; + if (te != null && clazz.isAssignableFrom(te.getClass())) { + return (TE) te; + } + } } + return null; } } diff --git a/src/main/java/slimeknights/mantle/inventory/SimpleChestContainer.java b/src/main/java/slimeknights/mantle/inventory/SimpleChestContainer.java index aebf646..6a00788 100644 --- a/src/main/java/slimeknights/mantle/inventory/SimpleChestContainer.java +++ b/src/main/java/slimeknights/mantle/inventory/SimpleChestContainer.java @@ -2,14 +2,17 @@ package slimeknights.mantle.inventory; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.IInventory; -import net.minecraft.inventory.container.ContainerType; import net.minecraft.inventory.container.Slot; +import net.minecraft.network.PacketBuffer; import slimeknights.mantle.tileentity.InventoryTileEntity; +/* +NEVER USED, PENDING REMOVAL? + */ public class SimpleChestContainer extends BaseContainer { - public SimpleChestContainer(ContainerType containerType, int windowId, InventoryTileEntity tile, int rows, int columns, PlayerInventory playerInventory) { - super(containerType, windowId, tile); + public SimpleChestContainer(int windowId, PlayerInventory playerInventory, InventoryTileEntity tile, int rows, int columns) { + super(null, windowId, playerInventory, tile); int index = 0; @@ -27,10 +30,22 @@ public class SimpleChestContainer extends BaseContainer { } // player inventory - this.addPlayerInventory(playerInventory, 17, 86); + this.addInventorySlots(); + } + + public SimpleChestContainer(int id, PlayerInventory inv, PacketBuffer buf) { + this(id, inv, getTileEntityFromBuf(buf, InventoryTileEntity.class), 0, 0); } protected Slot createSlot(IInventory inventory, int index, int x, int y) { return new Slot(inventory, index, x, y); } + + protected int getInventoryXOffset() { + return 17; + } + + protected int getInventoryYOffset() { + return 86; + } } diff --git a/src/main/java/slimeknights/mantle/util/TileEntityUtil.java b/src/main/java/slimeknights/mantle/util/TileEntityUtil.java new file mode 100644 index 0000000..6ba7c1f --- /dev/null +++ b/src/main/java/slimeknights/mantle/util/TileEntityUtil.java @@ -0,0 +1,52 @@ +package slimeknights.mantle.util; + +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockReader; +import net.minecraft.world.IWorldReader; +import slimeknights.mantle.Mantle; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class TileEntityUtil { + @Nullable + public static T getTileEntity(@Nonnull Class clazz, @Nullable IBlockReader world, @Nonnull BlockPos pos) { + return getTileEntity(clazz, world, pos, false); + } + + @Nullable + public static T getTileEntity(@Nonnull Class clazz, @Nullable IBlockReader world, @Nonnull BlockPos pos, boolean logWrongType) { + System.out.println("world: " + world + " pos: " + pos); + if (!isBlockLoaded(world, pos)) { + return null; + } + + //TODO: This causes freezes if being called from onLoad + TileEntity tile = world.getTileEntity(pos); + + System.out.println("world: " + world + " pos: " + pos + " tile: " + tile); + + if (tile == null) { + return null; + } + + if (clazz.isInstance(tile)) { + return clazz.cast(tile); + } else if (logWrongType) { + Mantle.logger.warn("Unexpected TileEntity class at {}, expected {}, but found: {}", pos, clazz, tile.getClass()); + } + + return null; + } + + public static boolean isBlockLoaded(@Nullable IBlockReader world, @Nonnull BlockPos pos) { + if (world == null) { + return false; + } + if (world instanceof IWorldReader) { + return ((IWorldReader) world).isBlockLoaded(pos); + } + return true; + } +}