+ Finished upgrade system. Now let's add some upgrades

This commit is contained in:
Nicolas BARBOTIN 2018-01-28 23:47:49 +01:00
parent 3a64796005
commit 614411eeb6
4 changed files with 99 additions and 8 deletions

View File

@ -250,6 +250,11 @@ public class GuiScreenConfig extends WDScreen {
}
}
@GuiSubscribe
public void onRemoveUpgrade(UpgradeGroup.ClickEvent ev) {
WebDisplays.NET_HANDLER.sendToServer(new SMessageScreenCtrl(tes, side, ev.getMouseOverStack()));
}
public boolean isFriendCheckbox(CheckBox cb) {
for(CheckBox box : friendBoxes) {
if(box == cb)

View File

@ -9,6 +9,7 @@ import net.minecraft.client.renderer.RenderItem;
import net.minecraft.item.ItemStack;
import net.montoyo.wd.client.gui.loading.JsonOWrapper;
import java.io.IOException;
import java.util.ArrayList;
public class UpgradeGroup extends BasicControl {
@ -17,6 +18,7 @@ public class UpgradeGroup extends BasicControl {
private int height;
private ArrayList<ItemStack> upgrades;
private ItemStack overStack;
private ItemStack clickStack;
private final RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
public UpgradeGroup() {
@ -29,6 +31,9 @@ public class UpgradeGroup extends BasicControl {
int x = this.x;
for(ItemStack is: upgrades) {
if(is == overStack)
fillRect(x, y, 16, 16, 0x80FF0000);
renderItem.renderItemAndEffectIntoGUI(mc.player, is, x, y);
renderItem.renderItemOverlayIntoGUI(font, is, x, y, null);
x += 18;
@ -77,15 +82,48 @@ public class UpgradeGroup extends BasicControl {
@Override
public void mouseMove(int mouseX, int mouseY) {
overStack = null;
if(upgrades != null) {
overStack = null;
if(mouseY >= y && mouseY <= y + 16 && mouseX >= x) {
mouseX -= x;
int sel = mouseX / 18;
if(mouseY >= y && mouseY <= y + 16 && mouseX >= x) {
mouseX -= x;
int sel = mouseX / 18;
if(sel < upgrades.size() && mouseX % 18 <= 16)
overStack = upgrades.get(sel);
if(sel < upgrades.size() && mouseX % 18 <= 16)
overStack = upgrades.get(sel);
}
}
}
@Override
public void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
if(mouseButton == 0)
clickStack = overStack;
}
@Override
public void mouseReleased(int mouseX, int mouseY, int state) {
if(state == 0 && clickStack != null) {
if(clickStack == overStack && upgrades.contains(clickStack)) //HOTFIX: Make sure it's actually in the list :p
parent.actionPerformed(new ClickEvent(this));
clickStack = null;
}
}
public static class ClickEvent extends Event<UpgradeGroup> {
private ItemStack clickStack;
private ClickEvent(UpgradeGroup src) {
source = src;
clickStack = src.clickStack;
}
public ItemStack getMouseOverStack() {
return clickStack;
}
}
}

View File

@ -623,4 +623,36 @@ public class TileEntityScreen extends TileEntity {
return scr.upgrades.stream().anyMatch((otherStack) -> itemAsUpgrade.isSameUpgrade(is, otherStack));
}
public void removeUpgrade(BlockSide side, ItemStack is) {
if(world.isRemote)
return;
Screen scr = getScreen(side);
if(scr == null) {
Log.error("Tried to remove an upgrade on invalid screen on side %s", side.toString());
return;
}
if(!(is.getItem() instanceof IUpgrade)) {
Log.error("Tried to remove a non-upgrade item %s to screen (%s does not implement IUpgrade)", safeName(is), is.getItem().getClass().getCanonicalName());
return;
}
int idxToRemove = -1;
IUpgrade itemAsUpgrade = (IUpgrade) is.getItem();
for(int i = 0; i < scr.upgrades.size(); i++) {
if(itemAsUpgrade.isSameUpgrade(is, scr.upgrades.get(i))) {
idxToRemove = i;
break;
}
}
if(idxToRemove >= 0) {
scr.upgrades.remove(idxToRemove);
WebDisplays.NET_HANDLER.sendToAllAround(CMessageScreenUpdate.upgrade(this, side), point());
} else
Log.warning("Tried to remove non-existing upgrade %s to screen %s at %s", safeName(is), side.toString(), pos.toString());
}
}

View File

@ -6,6 +6,7 @@ package net.montoyo.wd.net;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
@ -30,6 +31,7 @@ public class SMessageScreenCtrl implements IMessage, Runnable {
public static final int CTRL_SET_RIGHTS = 4;
public static final int CTRL_SET_RESOLUTION = 5;
public static final int CTRL_TYPE = 6;
public static final int CTRL_REMOVE_UPGRADE = 7;
private int ctrl;
private int dim;
@ -43,6 +45,7 @@ public class SMessageScreenCtrl implements IMessage, Runnable {
private Vector2i resolution;
private String text;
private BlockPos soundPos;
private ItemStack toRemove;
public SMessageScreenCtrl() {
}
@ -88,6 +91,14 @@ public class SMessageScreenCtrl implements IMessage, Runnable {
resolution = res;
}
public SMessageScreenCtrl(TileEntityScreen tes, BlockSide side, ItemStack toRem) {
ctrl = CTRL_REMOVE_UPGRADE;
dim = tes.getWorld().provider.getDimension();
pos = new Vector3i(tes.getPos());
this.side = side;
toRemove = toRem;
}
public static SMessageScreenCtrl type(TileEntityScreen tes, BlockSide side, String text, BlockPos soundPos) {
SMessageScreenCtrl ret = new SMessageScreenCtrl();
ret.ctrl = CTRL_TYPE;
@ -123,7 +134,8 @@ public class SMessageScreenCtrl implements IMessage, Runnable {
int sy = buf.readInt();
int sz = buf.readInt();
soundPos = new BlockPos(sx, sy, sz);
}
} else if(ctrl == CTRL_REMOVE_UPGRADE)
toRemove = ByteBufUtils.readItemStack(buf);
}
@Override
@ -147,7 +159,8 @@ public class SMessageScreenCtrl implements IMessage, Runnable {
buf.writeInt(soundPos.getX());
buf.writeInt(soundPos.getY());
buf.writeInt(soundPos.getZ());
}
} else if(ctrl == CTRL_REMOVE_UPGRADE)
ByteBufUtils.writeItemStack(buf, toRemove);
}
@Override
@ -205,6 +218,9 @@ public class SMessageScreenCtrl implements IMessage, Runnable {
} else if(ctrl == CTRL_TYPE) {
checkPermission(tes, ScreenRights.CLICK);
tes.type(side, text, soundPos);
} else if(ctrl == CTRL_REMOVE_UPGRADE) {
checkPermission(tes, ScreenRights.MANAGE_UPGRADES);
tes.removeUpgrade(side, toRemove);
} else
Log.info("SMessageScreenCtrl: TODO"); //TODO: other ctrl messages
}