From d09f344f1df60d878fdec2e7e24e9ae6f27dbe22 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 31 Aug 2015 03:58:35 +0200 Subject: [PATCH] Made it so that the info book spawns when the player crafts an Actually Additions Item for the first time --- .../actuallyadditions/event/CraftEvent.java | 13 ++++ .../network/PacketGiveBook.java | 60 +++++++++++++++++++ .../network/PacketHandler.java | 1 + 3 files changed, 74 insertions(+) create mode 100644 src/main/java/ellpeck/actuallyadditions/network/PacketGiveBook.java diff --git a/src/main/java/ellpeck/actuallyadditions/event/CraftEvent.java b/src/main/java/ellpeck/actuallyadditions/event/CraftEvent.java index fc0c92c58..b059820fb 100644 --- a/src/main/java/ellpeck/actuallyadditions/event/CraftEvent.java +++ b/src/main/java/ellpeck/actuallyadditions/event/CraftEvent.java @@ -14,6 +14,12 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; 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.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.item.ItemStack; @@ -22,6 +28,13 @@ public class CraftEvent{ @SubscribeEvent 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); + } + } } public static void checkAchievements(ItemStack gotten, EntityPlayer player, int type){ diff --git a/src/main/java/ellpeck/actuallyadditions/network/PacketGiveBook.java b/src/main/java/ellpeck/actuallyadditions/network/PacketGiveBook.java new file mode 100644 index 000000000..315a206b7 --- /dev/null +++ b/src/main/java/ellpeck/actuallyadditions/network/PacketGiveBook.java @@ -0,0 +1,60 @@ +/* + * This file ("PacketGiveBook.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 ellpeck.actuallyadditions.items.InitItems; +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import net.minecraftforge.common.DimensionManager; + +public class PacketGiveBook implements IMessage{ + + @SuppressWarnings("unused") + public PacketGiveBook(){ + + } + + private EntityPlayer player; + + public PacketGiveBook(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 + public IMessage onMessage(PacketGiveBook message, MessageContext ctx){ + EntityItem entityItem = new EntityItem(message.player.worldObj, message.player.posX, message.player.posY, message.player.posZ, new ItemStack(InitItems.itemLexicon)); + entityItem.delayBeforeCanPickup = 0; + message.player.worldObj.spawnEntityInWorld(entityItem); + + return null; + } + } +} diff --git a/src/main/java/ellpeck/actuallyadditions/network/PacketHandler.java b/src/main/java/ellpeck/actuallyadditions/network/PacketHandler.java index ed03d2d3e..6f91cc6da 100644 --- a/src/main/java/ellpeck/actuallyadditions/network/PacketHandler.java +++ b/src/main/java/ellpeck/actuallyadditions/network/PacketHandler.java @@ -30,5 +30,6 @@ public class PacketHandler{ theNetwork.registerMessage(PacketSyncerToClient.Handler.class, PacketSyncerToClient.class, 1, Side.CLIENT); 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); } }