Skip to content

Commit

Permalink
fix crashes related to offhand use
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Mar 28, 2016
1 parent c9f049c commit 563c19a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
32 changes: 21 additions & 11 deletions src/main/java/com/lothrazar/enderbook/ItemEnderBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;

public class ItemEnderBook extends Item
{
Expand Down Expand Up @@ -68,14 +65,28 @@ public static int getEmptySlotAndIncrement(ItemStack itemStack)
return empty;
}

public static void deleteWaypoint(ItemStack book, int slot)
private static ItemStack getPlayersBook(EntityPlayer player){

ItemStack book = player.getHeldItem(EnumHand.MAIN_HAND);
if(book == null || book.getItem() != ItemEnderBook.itemEnderBook){
book = player.getHeldItem(EnumHand.OFF_HAND);
}

if (book.getTagCompound() == null) {book.setTagCompound(new NBTTagCompound());}
return book;
}
public static void deleteWaypoint(EntityPlayer player, int slot)
{

ItemStack book = getPlayersBook(player);
book.getTagCompound().removeTag(KEY_LOC + "_" + slot);
}

public static void saveCurrentLocation(EntityPlayer player,ItemStack book, String name)
public static void saveCurrentLocation(EntityPlayer player, String name)
{
if (book.getTagCompound() == null) {book.setTagCompound(new NBTTagCompound());}

ItemStack book = getPlayersBook(player);


int id = getEmptySlotAndIncrement(book);//int slot = entityPlayer.inventory.currentItem + 1;

Expand All @@ -98,15 +109,16 @@ private static BookLocation getLocation(ItemStack stack, int slot)

public static void teleport(EntityPlayer player,int slot)// ItemStack enderBookInstance
{
ItemStack stack = player.getHeldItem(player.getActiveHand());
String csv = stack.getTagCompound().getString(ItemEnderBook.KEY_LOC + "_" + slot);
ItemStack book = getPlayersBook(player);

String csv = book.getTagCompound().getString(ItemEnderBook.KEY_LOC + "_" + slot);

if(csv == null || csv.isEmpty())
{
return;
}

BookLocation loc = getLocation(stack ,slot);
BookLocation loc = getLocation(book ,slot);
if(player.dimension != loc.dimension)
{
return;
Expand Down Expand Up @@ -178,8 +190,6 @@ public static void initEnderbook()
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer entityPlayer, EnumHand hand)
{
//ItemStack itemStack = entityPlayer.getHeldItem(entityPlayer.getActiveHand());

if (stack == null || stack.getItem() == null ) {
return new ActionResult<ItemStack>(EnumActionResult.FAIL, stack);
}
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/lothrazar/enderbook/ModEnderBook.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.lothrazar.enderbook;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.Action;
import net.minecraftforge.fml.client.event.ConfigChangedEvent.OnConfigChangedEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public IMessage onMessage(PacketDeleteButton message, MessageContext ctx)
{
EntityPlayer player = ((NetHandlerPlayServer)ctx.netHandler).playerEntity;

ItemEnderBook.deleteWaypoint(player.getHeldItem(player.getActiveHand()), message.slot);
ItemEnderBook.deleteWaypoint(player, message.slot);

//http://minecraft.gamepedia.com/Sounds.json
UtilSound.playSound(player.getEntityWorld(), player.getPosition(), SoundEvents.item_chorus_fruit_teleport, SoundCategory.PLAYERS);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/lothrazar/enderbook/PacketNewButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public IMessage onMessage(PacketNewButton message, MessageContext ctx)
}
*/
//it now passes the stack, in case the players hand becomes null/empty at some point during process
ItemEnderBook.saveCurrentLocation(player,player.getHeldItem(player.getActiveHand()),message.name);


ItemEnderBook.saveCurrentLocation(player,message.name);


return null;
Expand Down

0 comments on commit 563c19a

Please sign in to comment.