ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/event/PlayerObtainEvents.java

81 lines
3.6 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2015-12-30 22:02:15 +01:00
* This file ("PlayerObtainEvents.java") is part of the Actually Additions Mod for Minecraft.
2015-08-29 14:33:25 +02:00
* 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-11-02 20:55:19 +01:00
* © 2015 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2015-12-30 22:02:15 +01:00
package de.ellpeck.actuallyadditions.event;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
2015-12-30 22:02:15 +01:00
import de.ellpeck.actuallyadditions.achievement.InitAchievements;
import de.ellpeck.actuallyadditions.achievement.TheAchievements;
import de.ellpeck.actuallyadditions.config.values.ConfigBoolValues;
import de.ellpeck.actuallyadditions.items.InitItems;
import de.ellpeck.actuallyadditions.misc.WorldData;
import de.ellpeck.actuallyadditions.util.ModUtil;
import de.ellpeck.actuallyadditions.util.playerdata.PersistentServerData;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
2015-07-01 21:32:48 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
2015-04-19 01:50:02 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import java.util.Locale;
2015-11-23 19:17:17 +01:00
public class PlayerObtainEvents{
2015-03-29 15:29:05 +02:00
@SubscribeEvent
public void onCraftedEvent(PlayerEvent.ItemCraftedEvent event){
2015-12-09 15:47:57 +01:00
checkAchievements(event.crafting, event.player, InitAchievements.Type.CRAFTING);
2015-09-20 20:44:20 +02:00
if(ConfigBoolValues.GIVE_BOOKLET_ON_FIRST_CRAFT.isEnabled()){
if(!event.player.worldObj.isRemote && event.crafting.getItem() != InitItems.itemBooklet){
String itemName = Item.itemRegistry.getNameForObject(event.crafting.getItem());
String blockName = Block.blockRegistry.getNameForObject(Block.getBlockFromItem(event.crafting.getItem()));
if((itemName != null && itemName.toLowerCase(Locale.ROOT).contains(ModUtil.MOD_ID_LOWER)) || (blockName != null && blockName.toLowerCase(Locale.ROOT).contains(ModUtil.MOD_ID_LOWER))){
NBTTagCompound compound = PersistentServerData.getDataFromPlayer(event.player);
if(compound != null && !compound.getBoolean("BookGottenAlready")){
compound.setBoolean("BookGottenAlready", true);
WorldData.makeDirty();
EntityItem entityItem = new EntityItem(event.player.worldObj, event.player.posX, event.player.posY, event.player.posZ, new ItemStack(InitItems.itemBooklet));
entityItem.delayBeforeCanPickup = 0;
event.player.worldObj.spawnEntityInWorld(entityItem);
}
2015-09-20 20:44:20 +02:00
}
}
}
2015-04-19 01:50:02 +02:00
}
2015-10-03 10:19:40 +02:00
2015-12-09 15:47:57 +01:00
public static void checkAchievements(ItemStack gotten, EntityPlayer player, InitAchievements.Type type){
2015-10-03 10:19:40 +02:00
for(int i = 0; i < TheAchievements.values().length; i++){
TheAchievements ach = TheAchievements.values()[i];
if(ach.type == type){
if(gotten != null && ach.ach.theItemStack != null && gotten.getItem() == ach.ach.theItemStack.getItem()){
if(gotten.getItemDamage() == ach.ach.theItemStack.getItemDamage()){
player.addStat(ach.ach, 1);
}
}
}
}
}
2015-12-01 19:48:09 +01:00
@SubscribeEvent
public void onSmeltedEvent(PlayerEvent.ItemSmeltedEvent event){
2015-12-09 15:47:57 +01:00
checkAchievements(event.smelting, event.player, InitAchievements.Type.SMELTING);
2015-12-01 19:48:09 +01:00
}
@SubscribeEvent
public void onPickupEvent(PlayerEvent.ItemPickupEvent event){
2015-12-09 15:47:57 +01:00
checkAchievements(event.pickedUp.getEntityItem(), event.player, InitAchievements.Type.PICK_UP);
2015-12-01 19:48:09 +01:00
}
}