use of lumien.randomthings.handler.EnderLetterHandler.EnderMailboxInventory in project Random-Things by lumien231.
the class BlockEnderMailbox method onBlockActivated.
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
if (!worldIn.isRemote) {
ItemStack heldItem = playerIn.getHeldItemMainhand();
if (playerIn.isSneaking() && heldItem.getItem() == ModItems.enderLetter) {
NBTTagCompound compound;
if ((compound = heldItem.getTagCompound()) != null) {
if (compound.hasKey("receiver") && !compound.getBoolean("received")) {
GameProfile playerProfile = worldIn.getMinecraftServer().getPlayerProfileCache().getGameProfileForUsername(compound.getString("receiver"));
if (playerProfile != null && playerProfile.getId() != null) {
EnderMailboxInventory mailboxInventory = EnderLetterHandler.get(worldIn).getOrCreateInventoryForPlayer(playerProfile.getId());
for (int slot = 0; slot < mailboxInventory.getSizeInventory(); slot++) {
if (mailboxInventory.getStackInSlot(slot).isEmpty()) {
ItemStack sendingLetter = heldItem.copy();
heldItem.shrink(1);
sendingLetter.getTagCompound().setBoolean("received", true);
sendingLetter.getTagCompound().setString("sender", playerIn.getGameProfile().getName());
mailboxInventory.setInventorySlotContents(slot, sendingLetter);
playerIn.world.playSound(null, pos, SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.BLOCKS, 1, 1);
return true;
}
}
playerIn.sendMessage(new TextComponentTranslation("item.enderLetter.noSpace").setStyle(new Style().setColor(TextFormatting.DARK_PURPLE)));
} else {
playerIn.sendMessage(new TextComponentTranslation("item.enderLetter.noPlayer", compound.getString("receiver")).setStyle(new Style().setColor(TextFormatting.DARK_PURPLE)));
}
}
}
return true;
}
TileEntityEnderMailbox te = (TileEntityEnderMailbox) worldIn.getTileEntity(pos);
if (te.getOwner() != null) {
if (te.getOwner().equals(playerIn.getGameProfile().getId())) {
playerIn.openGui(RandomThings.instance, GuiIds.ENDER_MAILBOX, worldIn, pos.getX(), pos.getY(), pos.getZ());
} else {
playerIn.sendMessage(new TextComponentTranslation("block.enderMailbox.owner").setStyle(new Style().setColor(TextFormatting.RED)));
}
}
}
return true;
}
Aggregations