Skip to content

Commit

Permalink
fix some bugs/provide some info in gui
Browse files Browse the repository at this point in the history
  • Loading branch information
turtton committed Jul 7, 2021
1 parent 5d2b635 commit 487d59c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos,

@Override
public EnumBlockRenderType getRenderType(IBlockState state) {
return (state.getValue(MASKED) == true) ? EnumBlockRenderType.ENTITYBLOCK_ANIMATED : EnumBlockRenderType.MODEL;
return (state.getValue(MASKED) == true) ? EnumBlockRenderType.INVISIBLE : EnumBlockRenderType.MODEL;
}

@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, FACING, MASKED);
}
}

@Override
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemSplashPotion;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
Expand All @@ -17,11 +19,9 @@ public class ContainerAirDispersal extends BaseContainer {

public ContainerAirDispersal(InventoryPlayer invPlayer, GRTileEntityAirDispersal tileInventory) {
this.tileInventory = tileInventory;
INPUT_SLOTS = 2;
attachPlayerInventory(invPlayer);
IItemHandler itemhandlerinput = tileInventory.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
this.addSlotToContainer(new ContainerAirDispersal.Potion(itemhandlerinput, 0, 55, 35));
this.addSlotToContainer(new ContainerAirDispersal.Block(itemhandlerinput, 1, 8, 60));
}

@Override
Expand All @@ -43,6 +43,12 @@ public void detectAndSendChanges() {
}
}

@SideOnly(Side.CLIENT)
@Override
public void updateProgressBar(int id, int data) {
tileInventory.setField(id, data);
}

static class Potion extends SlotItemHandler {
public Potion(IItemHandler inv, int index, int x, int y) {
super(inv, index, x, y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -23,14 +24,15 @@
import net.minecraftforge.items.ItemStackHandler;

public class GRTileEntityAirDispersal extends GRTileEntityBasicEnergyReceiver implements ITickable {
public static final int OFF = 0;
public static final int UNLOCKED = 1;
public static final int LOCKED = 2;
public static final int RUNNING =3 ;

public static final int UNLOCKED = 0;
public static final int LOCKED = 1;

private int time;
private int timeLeft;
private final Item[] lock = new Item[4];
private int state;

private ItemStack maskBlock = ItemStack.EMPTY;


public GRTileEntityAirDispersal() {
Expand All @@ -43,13 +45,13 @@ public GRTileEntityAirDispersal(String name) {

public GRTileEntityAirDispersal(String name, boolean a) {
super(name);
state = OFF;
state = UNLOCKED;
storage = new CustomEnergyStorage(1000);
NUMBER_OF_FIELDS = 5;
}

public boolean isRunning() {
return state == RUNNING;
return state == LOCKED && world.isBlockPowered(getPos());
}

public int getState() {
Expand All @@ -67,35 +69,38 @@ public void setTimeLeft(int ticks) {
public int getTimeLeft() {
return timeLeft;
}

public void setTime(int time) {
this.time = time;
}

public boolean isLocked() {
return state == LOCKED || state == RUNNING;
return state == LOCKED;
}

public void setLocked(boolean l) {
if (l && (!isPrimed() || !hasPower())) return;
state = (l) ? LOCKED : UNLOCKED;
if (state == LOCKED && world.isBlockPowered(getPos())) state = RUNNING;
}

@Override
public void update() {
if (hasPower())
world.setBlockState(getPos(), world.getBlockState(getPos()).withProperty(AirDispersal.MASKED, isLocked() && (guiStackHandler.getStackInSlot(1) != ItemStack.EMPTY)));
if (storage.getEnergyStored() == storage.getMaxEnergyStored() && state == OFF)
state = UNLOCKED;
if (state == RUNNING) {
// if (hasPower())
world.setBlockState(getPos(), world.getBlockState(getPos()).withProperty(AirDispersal.MASKED, !maskBlock.isEmpty()));

if (isRunning()) {
timeLeft--;
if (timeLeft <= 0) {
state = UNLOCKED;
timeLeft = time;
throwPotion(); //must be after state change
markDirty();

}
}
}

public boolean hasPower() {
return state != OFF;
return storage.getEnergyStored() > 0;
}

public boolean isPrimed() {
Expand All @@ -114,7 +119,7 @@ public void throwPotion() {
if (!isPrimed()) return;
if (world.isRemote) return;

state = OFF;
state = UNLOCKED;
timeLeft = 0;
clearLock();
storage.extractEnergy(1000, false);
Expand Down Expand Up @@ -142,6 +147,7 @@ public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
compound.setTag("inventory", guiStackHandler.serializeNBT());
compound.setInteger("state", state);
compound.setInteger("time", time);
compound.setInteger("timeleft", timeLeft);
compound.setString("lock0", (lock[0] != null) ? lock[0].getRegistryName().toString() : "");
compound.setString("lock1", (lock[1] != null) ? lock[1].getRegistryName().toString() : "");
Expand All @@ -155,6 +161,7 @@ public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
guiStackHandler.deserializeNBT(compound.getCompoundTag("inventory"));
state = compound.getInteger("state");
time = compound.getInteger("time");
timeLeft = compound.getInteger("timeleft");
lock[0] = Item.getByNameOrId(compound.getString("lock0"));
lock[1] = Item.getByNameOrId(compound.getString("lock1"));
Expand All @@ -163,7 +170,7 @@ public void readFromNBT(NBTTagCompound compound) {
}

//These are the actual slots, used by GUI, unlimited access
private ItemStackHandler guiStackHandler = new ItemStackHandler(2) {
private ItemStackHandler guiStackHandler = new ItemStackHandler(1) {
@Override
protected void onContentsChanged(int slot) {
markDirty();
Expand All @@ -175,7 +182,7 @@ public int getSlotLimit(int slot) {
}

public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (state == LOCKED || state == RUNNING) return ItemStack.EMPTY;
if (state == LOCKED) return ItemStack.EMPTY;
return super.extractItem(slot, amount, simulate);
};

Expand All @@ -185,33 +192,31 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) {
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && facing == EnumFacing.DOWN) return false;
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return true;
else if (capability == CapabilityEnergy.ENERGY) return true;
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return false;
return super.hasCapability(capability, facing);
}

@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
if (capability == CapabilityEnergy.ENERGY) return (T) storage;
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
if (facing == null) return (T) guiStackHandler;
return null;
return (T) guiStackHandler;
}

return super.getCapability(capability, facing);
}


private static final byte TIME_FIELD_ID = 3;
private static final byte STATE_FIELD_ID = 4;

@Override
public int getField(int id) {
if (id == STATE_FIELD_ID) return this.state;
if (id == TIME_FIELD_ID) return this.timeLeft;
return super.getField(id);
}

@Override
public void setField(int id, int value) {
if (id == STATE_FIELD_ID) this.state = value;
else if (id == TIME_FIELD_ID) this.timeLeft = value;
Expand All @@ -224,7 +229,11 @@ public void clearLock() {
}

public ItemStack maskBlock() {
return guiStackHandler.getStackInSlot(1);
return maskBlock;
}

public void setMaskBlock(ItemStack item) {
maskBlock = item;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
Expand All @@ -37,9 +38,12 @@ public GuiAirDispersal(InventoryPlayer invPlayer, GRTileEntityAirDispersal tileI
public void initGui() {
super.initGui();
initNoPower = !tileEntity.hasPower();
if (!initNoPower)
this.buttonList.add(new GuiButton( 1, guiLeft+109, guiTop+53, 58, 20, tileEntity.isLocked() ? "Unlock" : "Lock"));
}
if (!initNoPower) {
this.buttonList.add(getButton());
} else {
buttonList.add(getButton("NoPower"));
}
}

@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int x, int y) {
Expand All @@ -61,12 +65,14 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int x, int y)
}
}

ItemStack mask = tileEntity.maskBlock();
if(!mask.isEmpty()) itemRender.renderItemIntoGUI(mask, guiLeft+8, guiTop+60);

Item[] lock = (tileEntity.isLocked()) ? guess : tileEntity.getLock();
if (lock[0] != null) itemRender.renderItemIntoGUI(new ItemStack(lock[0], 1), guiLeft+87, guiTop+9);
if (lock[1] != null) itemRender.renderItemIntoGUI(new ItemStack(lock[1], 1), guiLeft+108, guiTop+9);
if (lock[2] != null) itemRender.renderItemIntoGUI(new ItemStack(lock[2], 1), guiLeft+129, guiTop+9);
if (lock[3] != null) itemRender.renderItemIntoGUI(new ItemStack(lock[3], 1), guiLeft+150, guiTop+9);

}

@Override
Expand Down Expand Up @@ -101,25 +107,39 @@ private int digitsToTime(int[] digits) {

@Override
protected void actionPerformed(GuiButton button) throws IOException {
buttonList.clear();
if (tileEntity.isLocked()) {
Item[] lock = tileEntity.getLock();
for (int i=0;i<4;i++)
if (lock[i] != null && lock[i] != guess[i]) return;
for (int i=0;i<4;i++) {
if (lock[i] != null && lock[i] != guess[i]) {
buttonList.add(getButton("InvalidPass!"));
return;
}
}

//success!
tileEntity.setLocked(false);
tileEntity.clearLock();
} else {
if (!tileEntity.isPrimed()) {
buttonList.add(getButton("NoPotion!"));
return;
} else if (!tileEntity.hasPower()) {
buttonList.add(getButton("NoPower!"));
return;
}

tileEntity.setLocked(true);
}
GeneticsRebornPacketHandler.INSTANCE.sendToServer(new GuiMessage(tileEntity, 4, tileEntity.getState()));
this.buttonList.clear();
this.buttonList.add(new GuiButton( 1, guiLeft+109, guiTop+53, 58, 20, tileEntity.isLocked() ? "Unlock" : "Lock"));
this.buttonList.add(getButton());
}

private void addTicks(int pos) {
int[] digits = timeToDigits(tileEntity.timeLeft());
if (pos == 2) digits[pos] = (digits[pos] + 1) % 6;
else digits[pos] = (digits[pos] + 1) % 10;
tileEntity.setTime(digitsToTime(digits));
tileEntity.setTimeLeft(digitsToTime(digits));
GeneticsRebornPacketHandler.INSTANCE.sendToServer(new GuiMessage(tileEntity, 3, tileEntity.getTimeLeft()));
}
Expand All @@ -129,22 +149,39 @@ private void setLock(int pos) {
if (tileEntity.isLocked()) guess[pos] = item;
else tileEntity.setLock(pos, item);
}


private void setMask() {
ItemStack item = Minecraft.getMinecraft().player.inventory.getItemStack();
if (item.getItem() instanceof ItemBlock || item.isEmpty()) {
tileEntity.setMaskBlock(item);
}
}

private GuiButton getButton() {
return getButton(tileEntity.isLocked() ? "Unlock" : "Lock");
}

private GuiButton getButton(String name) {
return new GuiButton( 1, guiLeft+109, guiTop+53, 58, 20, name);
}

@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
super.mouseClicked(mouseX, mouseY, mouseButton);

if ((isPointInRegion(87, 9, 16, 16, mouseX, mouseY) && (mouseButton == 0))) setLock(0);
if ((isPointInRegion(108, 9, 16, 16, mouseX, mouseY) && (mouseButton == 0))) setLock(1);
if ((isPointInRegion(129, 9, 16, 16, mouseX, mouseY) && (mouseButton == 0))) setLock(2);
if ((isPointInRegion(150, 9, 16, 16, mouseX, mouseY) && (mouseButton == 0))) setLock(3);

if (tileEntity.isLocked()) return;

if ((isPointInRegion(8, 60, 16, 16, mouseX, mouseY) && (mouseButton == 0))) setMask();

if ((isPointInRegion(110, 33, 14, 19, mouseX, mouseY) && (mouseButton == 0))) addTicks(0);
if ((isPointInRegion(124, 33, 14, 19, mouseX, mouseY) && (mouseButton == 0))) addTicks(1);
if ((isPointInRegion(138, 33, 14, 19, mouseX, mouseY) && (mouseButton == 0))) addTicks(2);
if ((isPointInRegion(152, 33, 14, 19, mouseX, mouseY) && (mouseButton == 0))) addTicks(3);

if ((isPointInRegion(87, 9, 16, 16, mouseX, mouseY) && (mouseButton == 0))) setLock(0);
if ((isPointInRegion(108, 9, 16, 16, mouseX, mouseY) && (mouseButton == 0))) setLock(1);
if ((isPointInRegion(129, 9, 16, 16, mouseX, mouseY) && (mouseButton == 0))) setLock(2);
if ((isPointInRegion(150, 9, 16, 16, mouseX, mouseY) && (mouseButton == 0))) setLock(3);
}


Expand Down

1 comment on commit 487d59c

@turtton
Copy link
Owner Author

@turtton turtton commented on 487d59c Jul 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.