Made it so that the info book spawns when the player crafts an Actually Additions Item for the first time

This commit is contained in:
Ellpeck 2015-08-31 03:58:35 +02:00
parent dedae49689
commit d09f344f1d
3 changed files with 74 additions and 0 deletions

View file

@ -14,6 +14,12 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent; import cpw.mods.fml.common.gameevent.PlayerEvent;
import ellpeck.actuallyadditions.achievement.InitAchievements; import ellpeck.actuallyadditions.achievement.InitAchievements;
import ellpeck.actuallyadditions.achievement.TheAchievements; 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.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -22,6 +28,13 @@ public class CraftEvent{
@SubscribeEvent @SubscribeEvent
public void onCraftedEvent(PlayerEvent.ItemCraftedEvent event){ public void onCraftedEvent(PlayerEvent.ItemCraftedEvent event){
checkAchievements(event.crafting, event.player, InitAchievements.CRAFTING_ACH); 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){ public static void checkAchievements(ItemStack gotten, EntityPlayer player, int type){

View file

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

View file

@ -30,5 +30,6 @@ public class PacketHandler{
theNetwork.registerMessage(PacketSyncerToClient.Handler.class, PacketSyncerToClient.class, 1, Side.CLIENT); theNetwork.registerMessage(PacketSyncerToClient.Handler.class, PacketSyncerToClient.class, 1, Side.CLIENT);
theNetwork.registerMessage(PacketGuiNumber.Handler.class, PacketGuiNumber.class, 2, Side.SERVER); theNetwork.registerMessage(PacketGuiNumber.Handler.class, PacketGuiNumber.class, 2, Side.SERVER);
theNetwork.registerMessage(PacketGuiString.Handler.class, PacketGuiString.class, 3, Side.SERVER); theNetwork.registerMessage(PacketGuiString.Handler.class, PacketGuiString.class, 3, Side.SERVER);
theNetwork.registerMessage(PacketGiveBook.Handler.class, PacketGiveBook.class, 4, Side.SERVER);
} }
} }