From 657cde463f8d1b5fb60b2d1c9380ca8814d5cc71 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 20 Sep 2015 18:53:32 +0200 Subject: [PATCH] Fixed tooltip bugging sometimes & giving a player the book on crafting --- .../actuallyadditions/event/CraftEvent.java | 11 ++-- .../actuallyadditions/event/TooltipEvent.java | 54 ++++++++-------- .../network/PacketCheckBook.java | 61 +++++++++++++++++++ .../network/PacketHandler.java | 1 + 4 files changed, 95 insertions(+), 32 deletions(-) create mode 100644 src/main/java/ellpeck/actuallyadditions/network/PacketCheckBook.java diff --git a/src/main/java/ellpeck/actuallyadditions/event/CraftEvent.java b/src/main/java/ellpeck/actuallyadditions/event/CraftEvent.java index b059820fb..212a39659 100644 --- a/src/main/java/ellpeck/actuallyadditions/event/CraftEvent.java +++ b/src/main/java/ellpeck/actuallyadditions/event/CraftEvent.java @@ -15,12 +15,12 @@ import cpw.mods.fml.common.gameevent.PlayerEvent; import ellpeck.actuallyadditions.achievement.InitAchievements; import ellpeck.actuallyadditions.achievement.TheAchievements; import ellpeck.actuallyadditions.items.InitItems; -import ellpeck.actuallyadditions.network.PacketGiveBook; +import ellpeck.actuallyadditions.network.PacketCheckBook; import ellpeck.actuallyadditions.network.PacketHandler; import ellpeck.actuallyadditions.util.INameableItem; -import ellpeck.actuallyadditions.util.PersistantVariables; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; public class CraftEvent{ @@ -29,10 +29,9 @@ public class CraftEvent{ public void onCraftedEvent(PlayerEvent.ItemCraftedEvent event){ checkAchievements(event.crafting, event.player, InitAchievements.CRAFTING_ACH); - if(event.player.worldObj.isRemote && event.crafting.getItem() != InitItems.itemLexicon && (event.crafting.getItem() instanceof INameableItem || Block.getBlockFromItem(event.crafting.getItem()) instanceof INameableItem)){ - if(!PersistantVariables.getBoolean("BookGotten")){ - PacketHandler.theNetwork.sendToServer(new PacketGiveBook(event.player)); - PersistantVariables.setBoolean("BookGotten", true); + if(!event.player.worldObj.isRemote && event.crafting.getItem() != InitItems.itemLexicon && (event.crafting.getItem() instanceof INameableItem || Block.getBlockFromItem(event.crafting.getItem()) instanceof INameableItem)){ + if(event.player instanceof EntityPlayerMP){ + PacketHandler.theNetwork.sendTo(new PacketCheckBook(event.player), (EntityPlayerMP)event.player); } } } diff --git a/src/main/java/ellpeck/actuallyadditions/event/TooltipEvent.java b/src/main/java/ellpeck/actuallyadditions/event/TooltipEvent.java index 56c3ab697..315bf66be 100644 --- a/src/main/java/ellpeck/actuallyadditions/event/TooltipEvent.java +++ b/src/main/java/ellpeck/actuallyadditions/event/TooltipEvent.java @@ -47,37 +47,39 @@ public class TooltipEvent{ GuiScreen screen = Minecraft.getMinecraft().currentScreen; if(screen != null && !(screen instanceof GuiBooklet) && screen instanceof GuiContainer){ GuiContainer gui = (GuiContainer)screen; - for(Object o : gui.inventorySlots.inventorySlots){ - if(o instanceof Slot){ - Slot slot = (Slot)o; + if(gui.inventorySlots != null && gui.inventorySlots.inventorySlots != null && !gui.inventorySlots.inventorySlots.isEmpty()){ + for(Object o : gui.inventorySlots.inventorySlots){ + if(o instanceof Slot){ + Slot slot = (Slot)o; - int guiLeft = ReflectionHelper.getPrivateValue(GuiContainer.class, gui, 4); - int guiTop = ReflectionHelper.getPrivateValue(GuiContainer.class, gui, 5); - int mouseX = Mouse.getEventX()*gui.width/Minecraft.getMinecraft().displayWidth-guiLeft; - int mouseY = gui.height-Mouse.getEventY()*gui.height/Minecraft.getMinecraft().displayHeight-1-guiTop; + int guiLeft = ReflectionHelper.getPrivateValue(GuiContainer.class, gui, 4); + int guiTop = ReflectionHelper.getPrivateValue(GuiContainer.class, gui, 5); + int mouseX = Mouse.getEventX()*gui.width/Minecraft.getMinecraft().displayWidth-guiLeft; + int mouseY = gui.height-Mouse.getEventY()*gui.height/Minecraft.getMinecraft().displayHeight-1-guiTop; - if(mouseX >= slot.xDisplayPosition-1 && mouseY >= slot.yDisplayPosition-1 && mouseX <= slot.xDisplayPosition+16 && mouseY <= slot.yDisplayPosition+16){ - ItemStack stack = slot.getStack(); - if(stack != null){ - for(BookletPage page : InitBooklet.pagesWithItemStackData){ - if(page.getItemStackForPage() != null && page.getItemStackForPage().isItemEqual(stack)){ - int keyCode = KeyBinds.keybindOpenBooklet.getKeyCode(); - if(!ConfigBoolValues.NEED_BOOKLET_FOR_KEYBIND_INFO.isEnabled() || Minecraft.getMinecraft().thePlayer.inventory.hasItem(InitItems.itemLexicon)){ - event.toolTip.add(EnumChatFormatting.GOLD+StringUtil.localizeFormatted("booklet."+ModUtil.MOD_ID_LOWER+".keyToSeeRecipe", keyCode > 0 && keyCode < Keyboard.KEYBOARD_SIZE ? "'"+Keyboard.getKeyName(keyCode)+"'" : "[NONE]")); + if(mouseX >= slot.xDisplayPosition-1 && mouseY >= slot.yDisplayPosition-1 && mouseX <= slot.xDisplayPosition+16 && mouseY <= slot.yDisplayPosition+16){ + ItemStack stack = slot.getStack(); + if(stack != null){ + for(BookletPage page : InitBooklet.pagesWithItemStackData){ + if(page.getItemStackForPage() != null && page.getItemStackForPage().isItemEqual(stack)){ + int keyCode = KeyBinds.keybindOpenBooklet.getKeyCode(); + if(!ConfigBoolValues.NEED_BOOKLET_FOR_KEYBIND_INFO.isEnabled() || Minecraft.getMinecraft().thePlayer.inventory.hasItem(InitItems.itemLexicon)){ + event.toolTip.add(EnumChatFormatting.GOLD+StringUtil.localizeFormatted("booklet."+ModUtil.MOD_ID_LOWER+".keyToSeeRecipe", keyCode > 0 && keyCode < Keyboard.KEYBOARD_SIZE ? "'"+Keyboard.getKeyName(keyCode)+"'" : "[NONE]")); - //TODO Find a better method to do this eventually - if(Keyboard.isKeyDown(KeyBinds.keybindOpenBooklet.getKeyCode())){ - GuiBooklet book = new GuiBooklet(); - Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F)); - Minecraft.getMinecraft().displayGuiScreen(book); - book.openIndexEntry(page.getChapter().entry, InitBooklet.entries.indexOf(page.getChapter().entry)/GuiBooklet.BUTTONS_PER_PAGE+1, true); - book.openChapter(page.getChapter(), page); + //TODO Find a better method to do this eventually + if(Keyboard.isKeyDown(KeyBinds.keybindOpenBooklet.getKeyCode())){ + GuiBooklet book = new GuiBooklet(); + Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F)); + Minecraft.getMinecraft().displayGuiScreen(book); + book.openIndexEntry(page.getChapter().entry, InitBooklet.entries.indexOf(page.getChapter().entry)/GuiBooklet.BUTTONS_PER_PAGE+1, true); + book.openChapter(page.getChapter(), page); + } } + else{ + event.toolTip.addAll(Minecraft.getMinecraft().fontRenderer.listFormattedStringToWidth(EnumChatFormatting.DARK_RED+StringUtil.localizeFormatted("booklet."+ModUtil.MOD_ID_LOWER+".noBookletInInventory"), GuiBooklet.TOOLTIP_SPLIT_LENGTH)); + } + break; } - else{ - event.toolTip.addAll(Minecraft.getMinecraft().fontRenderer.listFormattedStringToWidth(EnumChatFormatting.DARK_RED+StringUtil.localizeFormatted("booklet."+ModUtil.MOD_ID_LOWER+".noBookletInInventory"), GuiBooklet.TOOLTIP_SPLIT_LENGTH)); - } - break; } } } diff --git a/src/main/java/ellpeck/actuallyadditions/network/PacketCheckBook.java b/src/main/java/ellpeck/actuallyadditions/network/PacketCheckBook.java new file mode 100644 index 000000000..6da0fb309 --- /dev/null +++ b/src/main/java/ellpeck/actuallyadditions/network/PacketCheckBook.java @@ -0,0 +1,61 @@ +/* + * This file ("PacketCheckBook.java") is part of the Actually Additions Mod for Minecraft. + * It is created and owned by Ellpeck and distributed + * under the Actually Additions License to be found at + * http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015 Ellpeck + */ + +package ellpeck.actuallyadditions.network; + +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import ellpeck.actuallyadditions.util.PersistantVariables; +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; +import net.minecraftforge.common.DimensionManager; + +public class PacketCheckBook implements IMessage{ + + @SuppressWarnings("unused") + public PacketCheckBook(){ + + } + + private EntityPlayer player; + + public PacketCheckBook(EntityPlayer player){ + this.player = player; + } + + @Override + public void fromBytes(ByteBuf buf){ + World world = DimensionManager.getWorld(buf.readInt()); + this.player = (EntityPlayer)world.getEntityByID(buf.readInt()); + } + + @Override + public void toBytes(ByteBuf buf){ + buf.writeInt(this.player.worldObj.provider.dimensionId); + buf.writeInt(this.player.getEntityId()); + } + + public static class Handler implements IMessageHandler{ + + @Override + @SideOnly(Side.CLIENT) + public IMessage onMessage(PacketCheckBook message, MessageContext ctx){ + if(!PersistantVariables.getBoolean("BookGotten")){ + PacketHandler.theNetwork.sendToServer(new PacketGiveBook(message.player)); + PersistantVariables.setBoolean("BookGotten", true); + } + return null; + } + } +} diff --git a/src/main/java/ellpeck/actuallyadditions/network/PacketHandler.java b/src/main/java/ellpeck/actuallyadditions/network/PacketHandler.java index 6f91cc6da..208c5085a 100644 --- a/src/main/java/ellpeck/actuallyadditions/network/PacketHandler.java +++ b/src/main/java/ellpeck/actuallyadditions/network/PacketHandler.java @@ -31,5 +31,6 @@ public class PacketHandler{ theNetwork.registerMessage(PacketGuiNumber.Handler.class, PacketGuiNumber.class, 2, Side.SERVER); theNetwork.registerMessage(PacketGuiString.Handler.class, PacketGuiString.class, 3, Side.SERVER); theNetwork.registerMessage(PacketGiveBook.Handler.class, PacketGiveBook.class, 4, Side.SERVER); + theNetwork.registerMessage(PacketCheckBook.Handler.class, PacketCheckBook.class, 5, Side.CLIENT); } }