Fixed tooltip bugging sometimes & giving a player the book on crafting

This commit is contained in:
Ellpeck 2015-09-20 18:53:32 +02:00
parent acc36b6e3e
commit 657cde463f
4 changed files with 95 additions and 32 deletions

View file

@ -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);
}
}
}

View file

@ -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;
}
}
}

View file

@ -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<PacketCheckBook, IMessage>{
@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;
}
}
}

View file

@ -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);
}
}