Skip to content

Commit

Permalink
Updated LocationPrintoutUpdate
Browse files Browse the repository at this point in the history
* re #4189
* The whole system could need a rewrite, but this should patch most problems
  • Loading branch information
HenryLoenwind committed Apr 30, 2017
1 parent 77d73f3 commit 9e5fd5f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,22 @@ public Object getServerGuiElement(int ID, EntityPlayer player, World world, int
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
if (GuiID.GUI_ID_LOCATION_PRINTOUT_CREATE.is(ID)) {

boolean foundPaper = false;
for (int paperIndex = 0; paperIndex < player.inventoryContainer.inventorySlots.size() && !foundPaper; paperIndex++) {
int foundPaper = -1;
for (int paperIndex = 0; paperIndex < player.inventoryContainer.inventorySlots.size() && foundPaper < 0; paperIndex++) {
ItemStack invItem = player.inventoryContainer.inventorySlots.get(paperIndex).getStack();
if (invItem != null && invItem.getItem() == Items.PAPER) {
player.inventoryContainer.inventorySlots.get(paperIndex).decrStackSize(1);
player.inventoryContainer.detectAndSendChanges();
foundPaper = true;
foundPaper = paperIndex;
}
}
if (!foundPaper) {
if (foundPaper < 0) {
player.addChatMessage(new TextComponentString(EnderIO.lang.localizeExact("item.itemLocationPrintout.chat.noPaper")));
return null;
}

TelepadTarget target = new TelepadTarget(new BlockPos(x, y, z), world.provider.getDimension());
ItemStack stack = new ItemStack(this);
target.writeToNBT(stack);
return new GuiLocationPrintout(stack);
return new GuiLocationPrintout(stack, foundPaper);
} else {
EnumHand hand = x == 0 ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND;
EntityEquipmentSlot slot = hand == EnumHand.MAIN_HAND ? EntityEquipmentSlot.MAINHAND : EntityEquipmentSlot.OFFHAND;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ public class GuiLocationPrintout extends GuiScreenBase {
private final ItemStack stack;
private final TelepadTarget target;
private final EntityEquipmentSlot slot;
private final int paperSlot;

private boolean isCancelled = false;

public GuiLocationPrintout(ItemStack stack) {
this(stack, null);
public GuiLocationPrintout(ItemStack stack, int paperSlot) {
this(stack, null, paperSlot);
}

public GuiLocationPrintout(EntityPlayer player, EntityEquipmentSlot slot) {
this(player.getItemStackFromSlot(slot), slot);
this(player.getItemStackFromSlot(slot), slot, -1);
}

public GuiLocationPrintout(ItemStack stack, EntityEquipmentSlot slot) {
public GuiLocationPrintout(ItemStack stack, EntityEquipmentSlot slot, int paperSlot) {

this.slot = slot;
this.stack = stack;
this.paperSlot = paperSlot;
target = TelepadTarget.readFromNBT(stack);

xSize = 176;
Expand Down Expand Up @@ -158,15 +160,15 @@ private void checkLabelForChange() {
target.setName(newTxt);
target.writeToNBT(stack);
if(slot != null) { //update as we go if the stack exists already
PacketUpdateLocationPrintout p = new PacketUpdateLocationPrintout(stack, slot);
PacketUpdateLocationPrintout p = new PacketUpdateLocationPrintout(stack, slot, paperSlot);
PacketHandler.INSTANCE.sendToServer(p);
}
}

@Override
public void onGuiClosed() {
if(slot == null && !isCancelled) {
PacketUpdateLocationPrintout p = new PacketUpdateLocationPrintout(stack, slot);
PacketUpdateLocationPrintout p = new PacketUpdateLocationPrintout(stack, slot, paperSlot);
PacketHandler.INSTANCE.sendToServer(p);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@

import com.enderio.core.common.util.ItemUtil;

import crazypants.enderio.teleport.telepad.TelepadTarget;
import crazypants.util.Prep;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Items;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import static crazypants.enderio.ModObject.itemLocationPrintout;

public class PacketUpdateLocationPrintout implements IMessage, IMessageHandler<PacketUpdateLocationPrintout, IMessage> {

private ItemStack stack;
private EntityEquipmentSlot slot;
private int paperSlot;

public PacketUpdateLocationPrintout() {
}

public PacketUpdateLocationPrintout(ItemStack stack, EntityEquipmentSlot slot) {
public PacketUpdateLocationPrintout(ItemStack stack, EntityEquipmentSlot slot, int paperSlot) {
this.stack = stack;
this.slot = slot;
this.paperSlot = paperSlot;
}

@Override
Expand All @@ -31,7 +39,8 @@ public void toBytes(ByteBuf buf) {
if(slot != null) {
ord = slot.ordinal();
}
buf.writeShort(ord);
buf.writeShort(ord);
buf.writeShort(paperSlot);
}

@Override
Expand All @@ -42,25 +51,64 @@ public void fromBytes(ByteBuf buf) {
slot = null;
} else {
slot = EntityEquipmentSlot.values()[ord];
}
}
paperSlot = buf.readShort();
}

@Override
public IMessage onMessage(PacketUpdateLocationPrintout message, MessageContext ctx) {
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
ItemStack stack = message.stack;
EntityEquipmentSlot slot = message.slot;
if(stack == null) {
int paperSlot = message.paperSlot;
if (Prep.isInvalid(stack) || stack.getItem() != itemLocationPrintout.getItem()) {
return null;
}
if(slot != null) {
player.setItemStackToSlot(slot, stack);
TelepadTarget telepadTarget = TelepadTarget.readFromNBT(stack);
if (telepadTarget == null) {
return null;
}
if (!ctx.getServerHandler().playerEntity.inventory.addItemStackToInventory(stack)) {
ItemUtil.spawnItemInWorldWithRandomMotion(player.worldObj, stack, player.getPosition());
}
if(slot != null) {
updatePrintout(player, slot, telepadTarget);
} else if (paperSlot >= 0) {
createPrintout(player, telepadTarget, paperSlot);
}
return null;
}

private void createPrintout(EntityPlayerMP player, TelepadTarget telepadTarget, int paperSlot) {
if (telepadTarget.getDimension() != player.worldObj.provider.getDimension()) {
return;
}
if (telepadTarget.getLocation().distanceSq(new BlockPos(player)) > 160 * 160) {
// ideally we'd want to raytrace this, but the difference between raytracing on the server and the client is just too big, especially over long
// distances...
return;
}
ItemStack invItem = player.inventoryContainer.inventorySlots.get(paperSlot).getStack();
if (Prep.isValid(invItem) && invItem.getItem() == Items.PAPER) {
player.inventoryContainer.inventorySlots.get(paperSlot).decrStackSize(1);
player.inventoryContainer.detectAndSendChanges();
ItemStack stack = new ItemStack(itemLocationPrintout.getItem());
telepadTarget.writeToNBT(stack);
if (!player.inventory.addItemStackToInventory(stack)) {
ItemUtil.spawnItemInWorldWithRandomMotion(player.worldObj, stack, player.getPosition());
}
}
}

private void updatePrintout(EntityPlayerMP player, EntityEquipmentSlot slot, TelepadTarget telepadTarget) {
ItemStack existingStack = player.getItemStackFromSlot(slot);
if (Prep.isInvalid(existingStack) || existingStack.getItem() != itemLocationPrintout.getItem()) {
return;
}
TelepadTarget existingTarget = TelepadTarget.readFromNBT(existingStack);
if (existingTarget == null) {
return;
}
existingTarget.setName(telepadTarget.getName());
existingTarget.writeToNBT(existingStack);
player.setItemStackToSlot(slot, existingStack);
}

}

0 comments on commit 9e5fd5f

Please sign in to comment.