Skip to content

Commit

Permalink
Implemented advanced drawbridge
Browse files Browse the repository at this point in the history
  • Loading branch information
fuj1n committed Sep 24, 2019
1 parent 58941f6 commit fd86782
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 28 deletions.
Expand Up @@ -2,12 +2,15 @@

import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.network.PacketDistributor;
import slimeknights.tmechworks.client.gui.components.ArrowWidget;
import slimeknights.tmechworks.common.blocks.tileentity.DrawbridgeTileEntity;
Expand All @@ -17,10 +20,13 @@
import slimeknights.tmechworks.common.network.packet.UpdatePlaceDirectionPacket;
import slimeknights.tmechworks.library.Util;

import java.util.List;

public class DrawbridgeScreen extends ContainerScreen<DrawbridgeContainer> {
public static final ResourceLocation SCREEN_LOCATION = new ResourceLocation("tmechworks", "textures/gui/drawbridge.png");
public static final ResourceLocation ADVANCED_LOCATION = new ResourceLocation("tmechworks", "textures/gui/drawbridge_advanced.png");

private final boolean isAdvanced;
public final boolean isAdvanced;
private final int slotCount;

public DrawbridgeScreen(DrawbridgeContainer container, PlayerInventory inventory, ITextComponent name) {
Expand All @@ -38,7 +44,17 @@ public static DrawbridgeScreen create(DrawbridgeContainer container, PlayerInven
protected void init() {
super.init();

ArrowWidget arrow = new ArrowWidget((this.width - this.xSize) / 2 + 110, (this.height - this.ySize) / 2 + 20, width, height, true, this::arrowClicked);
int aX = 0, aY = 0;

if(isAdvanced) {
aX = guiLeft + 192;
aY = guiTop + 10;
} else {
aX = (this.width - this.xSize) / 2 + 110;
aY = (this.height - this.ySize) / 2 + 20;
} //blit(guiLeft + 191, guiTop + 4, 0, 196, 63, 60);

ArrowWidget arrow = new ArrowWidget(aX, aY, width, height, true, this::arrowClicked);
updateSelection(arrow);
addButton(arrow);
}
Expand Down Expand Up @@ -67,19 +83,61 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i
GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
this.minecraft.getTextureManager().bindTexture(SCREEN_LOCATION);

blit(guiLeft, guiTop, 0, 0, xSize, ySize);
blit(guiLeft - 44, guiTop + ySize - 65, 0, 182, 47, 60);
blit(guiLeft, guiTop, 0, 0, xSize, ySize); // Background
blit(guiLeft - 44, guiTop + ySize - 65, 0, 182, 47, 60); // Upgrades cutout

if(!isAdvanced) {
// drawSlicedBox(guiLeft + 34, guiTop + 35, 18, 18, 17, 166); // Disguise slot
drawSlicedBox(guiLeft + 75, guiTop + 31, 26, 26, 17, 166); // Drawbridge slot
drawSlicedBox(guiLeft + 75, guiTop + 31, 26, 26, 17, 166); // Drawbridge slot
} else {
this.minecraft.getTextureManager().bindTexture(ADVANCED_LOCATION);

blit(guiLeft - 18, guiTop - 80, 0, 0, 213, 148); // Advanced cutout
blit(guiLeft + 191, guiTop + 4, 0, 196, 63, 60); // Arrow cutout

drawAdvancedSlots();
}
}

private void drawAdvancedSlots() {
for(Slot s : container.mainSlots){
blit(guiLeft + s.xPos - 1, guiTop + s.yPos - 1, 0, 166, 18, 18);
}
}

@Override
protected void renderHoveredToolTip(int mouseX, int mouseY) {
if(this.hoveredSlot == null)
return;

if(!isAdvanced || this.hoveredSlot.getHasStack()) {
super.renderHoveredToolTip(mouseX, mouseY);
} else if(hoveredSlot.inventory == getContainer().getTile().slots) {
renderTooltip(I18n.format(Util.prefix("gui.blocknum"), hoveredSlot.getSlotIndex() + 1), mouseX, mouseY);
}
}

@Override
public List<String> getTooltipFromItem(ItemStack stack) {
List<String> list = super.getTooltipFromItem(stack);

if(isAdvanced && hoveredSlot.inventory == getContainer().getTile().slots) {
list.add("");
list.add(TextFormatting.GRAY + I18n.format(Util.prefix("gui.blocknum"), hoveredSlot.getSlotIndex() + 1));
}

return list;
}

@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
super.drawGuiContainerForegroundLayer(mouseX, mouseY);

int screenTop = isAdvanced ? -80 : 0;

String s = title.getFormattedText();
font.drawString(s, xSize / 2F - font.getStringWidth(s) / 2F, 6, 4210752);
font.drawString(s, xSize / 2F - font.getStringWidth(s) / 2F, screenTop + 6, 4210752);

font.drawString(playerInventory.getDisplayName().getFormattedText(), 8, ySize - 96 + 2, 4210752);

float scale = .75F;
Expand Down
Expand Up @@ -3,6 +3,7 @@
public class MechworksConfig {
private static MechworksConfig instance = new MechworksConfig();

// Total extend length designed to be no higher than 64 blocks for advanced drawbridges, therefore going higher than 64 may produce weird results
public int drawbridgeExtendLength = 16;
public int drawbridgeExtendUpgradeValue = 16;
public float drawbridgeSpeed = 0.5F;
Expand Down
Expand Up @@ -112,7 +112,8 @@ public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List

@Override
public void setDefaultNBT(CompoundNBT nbt, CompoundNBT blockState) {
super.setDefaultNBT(nbt, blockState);
// Firestarter does not have an inventory
// super.setDefaultNBT(nbt, blockState);

if(!nbt.contains("extinguish"))
nbt.putBoolean("extinguish", true);
Expand Down
Expand Up @@ -66,7 +66,7 @@ public DrawbridgeTileEntity() {
super(MechworksContent.TileEntities.drawbridge, new TranslationTextComponent(Util.prefix("inventory.drawbridge")), UPGRADES_SIZE + 1);

upgrades = new FragmentedInventory(this, 0, UPGRADES_SIZE).overrideStackLimit(1).setValidItemsPredicate(stack -> stack.getItem() instanceof MachineUpgradeItem);
slots = new FragmentedInventory(this, UPGRADES_SIZE, 1).setValidItemsPredicate(stack -> stack.getItem() instanceof BlockItem);
slots = new FragmentedInventory(this, UPGRADES_SIZE, 1).setValidItemsPredicate(stack -> stack.getItem() instanceof BlockItem).overrideStackLimit(64);
}

@Override
Expand Down Expand Up @@ -111,7 +111,7 @@ public void tick() {
return;
}
} else {
if(extendedLength > stats.extendLength)
if (extendedLength > stats.extendLength)
extendedLength = stats.extendLength;

if (extendedLength <= 0) {
Expand Down Expand Up @@ -172,11 +172,10 @@ public void onRedstoneUpdate() {
World world = getWorld();

// Clamp extended state to nearest air block
for(int i = 1; i <= extendedLength; i++){
for (int i = 1; i <= extendedLength; i++) {
BlockPos pos = new BlockPos(getPos().getX() + dir.getXOffset() * i, getPos().getY() + dir.getYOffset() * i, getPos().getZ() + dir.getZOffset() * i);

if(world.isAirBlock(pos))
{
if (world.isAirBlock(pos)) {
extendedLength = i - 1;
break;
}
Expand All @@ -191,7 +190,7 @@ public int getSlot() {
if (!stats.isAdvanced)
return 0;
else
return isExtended ? extendedLength - 1 : extendedLength;
return isExtended ? extendedLength : extendedLength - 1;
}

public boolean placeBlock(BlockPos pos, ItemStack stack) {
Expand Down Expand Up @@ -255,12 +254,12 @@ public boolean breakBlock(BlockPos pos, int targetSlot) {
ItemStack target = drops.stream().filter(x -> stack.isItemEqual(x) && ItemStack.areItemStackTagsEqual(stack, x)).findFirst().orElse(ItemStack.EMPTY);

if (!target.isEmpty() && drops.remove(target)) {
int remainder = Math.max(stack.getCount() + target.getCount() - stack.getMaxStackSize(), 0);
int remainder = Math.max(stack.getCount() + target.getCount() - Math.min(stack.getMaxStackSize(), slots.getInventoryStackLimit()), 0);
int targetCount = target.getCount() - remainder;

stack.setCount(stack.getCount() + targetCount);

if(remainder > 0){
if (remainder > 0) {
target.setCount(remainder);
drops.add(target);
}
Expand Down Expand Up @@ -297,17 +296,18 @@ public void onStatsUpdated() {
BlockPos pos = getPos();

// Drop items in removed slots
for(int i = slots.getSizeInventory() - 1; i >= blockSlots; i--) {
for (int i = slots.getSizeInventory() - 1; i >= blockSlots; i--) {
ItemStack stack = slots.getStackInSlot(i);

if(!stack.isEmpty()) {
if (!stack.isEmpty()) {
InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), stack);
slots.setInventorySlotContents(i, ItemStack.EMPTY);
}
}

resize(UPGRADES_SIZE + blockSlots);
slots.resize(blockSlots);
slots.overrideStackLimit(stats.isAdvanced ? 1 : 64);

BlockState state = getWorld().getBlockState(getPos());
getWorld().setBlockState(getPos(), state.with(DrawbridgeBlock.ADVANCED, stats.isAdvanced));
Expand Down Expand Up @@ -525,7 +525,7 @@ public void updateFakePlayer(BlockPos pos) {
public void setInventorySlotContents(int slot, @Nonnull ItemStack itemstack) {
super.setInventorySlotContents(slot, itemstack);

if(upgrades.isSlotInInventory(slot))
if (upgrades.isSlotInInventory(slot))
computeStats();
}

Expand Down Expand Up @@ -661,7 +661,7 @@ private static class DrawbridgeTools {
}

public static ItemStack getByType(ToolType type) {
if(type == null)
if (type == null)
return PICKAXE;

switch (type.getName()) {
Expand Down
Expand Up @@ -18,7 +18,7 @@
import net.minecraftforge.fml.common.thread.EffectiveSide;
import slimeknights.mantle.tileentity.InventoryTileEntity;
import slimeknights.tmechworks.common.blocks.RedstoneMachineBlock;
import slimeknights.tmechworks.integration.IInformationProvider;
import slimeknights.tmechworks.integration.waila.IInformationProvider;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down
@@ -1,26 +1,39 @@
package slimeknights.tmechworks.common.inventory;

import com.google.common.collect.ImmutableList;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Slot;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3i;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import slimeknights.mantle.inventory.BaseContainer;
import slimeknights.tmechworks.common.MechworksContent;
import slimeknights.tmechworks.common.blocks.tileentity.DrawbridgeTileEntity;
import slimeknights.tmechworks.common.inventory.slots.ValidatingSlot;

import java.util.ArrayList;

public class DrawbridgeContainer extends BaseContainer<DrawbridgeTileEntity> {
public static final int ADVANCED_COLUMNS = 11;

public final PlayerInventory playerInventory;
public final int rows;

public final ImmutableList<Slot> mainSlots;

public DrawbridgeContainer(int id, PlayerInventory playerInventory, DrawbridgeTileEntity te) {
super(MechworksContent.Containers.drawbridge, id, te);

this.playerInventory = playerInventory;
te.openInventory(playerInventory.player);

addDrawbridgeSlots();
int slotCount = te.slots.getSizeInventory();
rows = MathHelper.ceil((float)te.slots.getSizeInventory() / ADVANCED_COLUMNS);
mainSlots = addDrawbridgeSlots();

for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
Expand All @@ -31,8 +44,34 @@ public DrawbridgeContainer(int id, PlayerInventory playerInventory, DrawbridgeTi
addPlayerInventory(playerInventory, 8, 84);
}

protected void addDrawbridgeSlots(){
addSlot(new ValidatingSlot(tile.slots, 0, 80, 36));
protected ImmutableList<Slot> addDrawbridgeSlots(){
if(!tile.stats.isAdvanced) {
return ImmutableList.of(addSlot(new ValidatingSlot(tile.slots, 0, 80, 36)));
} else {
ArrayList<Slot> slots = new ArrayList<>();

int slotCount = tile.slots.getSizeInventory();

final int left = -18 + 7;
final int width = 198;
final int height = 126;

final int top = -80 + 12 + height / 2 - rows * 18 / 2;

for(int y = 0; y < rows; y++) {
int cols = Math.min(slotCount - ADVANCED_COLUMNS * y, ADVANCED_COLUMNS);
int yCoord = top + y * 18;
int xStart = left + width / 2 - cols * 18 / 2;

for (int x = 0; x < cols; x++) {
int xCoord = xStart + x * 18;

slots.add(addSlot(new ValidatingSlot(tile.slots, y * ADVANCED_COLUMNS + x, xCoord + 1, yCoord + 1)));
}
}

return ImmutableList.copyOf(slots);
}
}

@OnlyIn(Dist.CLIENT)
Expand Down
@@ -1,6 +1,5 @@
package slimeknights.tmechworks.integration;

import com.google.common.collect.ImmutableList;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.gui.handlers.IGuiContainerHandler;
Expand All @@ -13,6 +12,7 @@
import slimeknights.tmechworks.common.MechworksContent;
import slimeknights.tmechworks.library.Util;

import java.util.ArrayList;
import java.util.List;

@JeiPlugin
Expand Down Expand Up @@ -41,8 +41,22 @@ public void registerGuiHandlers(IGuiHandlerRegistration registration) {
registration.addGuiContainerHandler(DrawbridgeScreen.class, new IGuiContainerHandler<DrawbridgeScreen>() {
@Override
public List<Rectangle2d> getGuiExtraAreas(DrawbridgeScreen gui) {
List<Rectangle2d> rects = new ArrayList<>();

int guiLeft = gui.getGuiLeft();
int guiTop = gui.getGuiTop();
int xSize = gui.getXSize();
int ySize = gui.getYSize();

// Upgrades UI cutout
return ImmutableList.of(new Rectangle2d(gui.getGuiLeft() - 44, gui.getGuiTop() + gui.getYSize() - 65, 47, 60));
rects.add(new Rectangle2d(guiLeft - 44, guiTop + ySize - 65, 47, 60));

if(gui.isAdvanced){
rects.add(new Rectangle2d(guiLeft - 18, guiTop - 80, 213, 148)); // Advanced UI
rects.add(new Rectangle2d(guiLeft + 191, guiTop + 4, 63, 60)); // Advanced arrows
}

return rects;
}
});
}
Expand Down
@@ -1,4 +1,4 @@
package slimeknights.tmechworks.integration;
package slimeknights.tmechworks.integration.waila;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
Expand Down
3 changes: 2 additions & 1 deletion src/resources/assets/tmechworks/lang/en_us.json
Expand Up @@ -47,5 +47,6 @@
"tmechworks:widget.arrow.low": "Place Low",

// GUI
"tmechworks:gui.upgrades": "Upgrades"
"tmechworks:gui.upgrades": "Upgrades",
"tmechworks:gui.blocknum": "Block #%d"
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fd86782

Please sign in to comment.