ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemWingsOfTheBats.java

163 lines
6.1 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("ItemWingsOfTheBats.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
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.items;
2015-07-17 06:51:19 +02:00
2016-07-03 20:57:00 +02:00
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
2016-07-03 20:57:00 +02:00
import de.ellpeck.actuallyadditions.mod.items.metalists.TheMiscItems;
import de.ellpeck.actuallyadditions.mod.util.Util;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.passive.EntityBat;
2015-07-17 06:51:19 +02:00
import net.minecraft.entity.player.EntityPlayer;
2016-07-03 20:57:00 +02:00
import net.minecraft.init.Blocks;
2015-07-17 06:51:19 +02:00
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
2016-07-03 20:57:00 +02:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
2015-07-17 06:51:19 +02:00
import java.util.ArrayList;
public class ItemWingsOfTheBats extends ItemBase{
2015-07-17 06:51:19 +02:00
/**
* A List containing all of the Players that can currently fly
* Used so that Flight from other Mods' Items doesn't get broken when
* these Wings aren't worn
2015-10-02 16:48:01 +02:00
* <p>
* Saves Remote Players separately to make de-synced Event Calling
* not bug out Capabilities when taking off the Wings
2015-10-02 16:48:01 +02:00
* <p>
2015-09-27 20:00:14 +02:00
* (Partially excerpted from Botania's Wing System by Vazkii (as I had fiddled around with the system and couldn't make it work) with permission, thanks!)
2015-07-17 06:51:19 +02:00
*/
2016-06-17 23:50:38 +02:00
public static final ArrayList<String> WINGED_PLAYERS = new ArrayList<String>();
2015-07-17 06:51:19 +02:00
public ItemWingsOfTheBats(String name){
super(name);
2015-07-17 06:51:19 +02:00
this.setMaxStackSize(1);
2016-07-03 20:57:00 +02:00
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void onLogOutEvent(PlayerEvent.PlayerLoggedOutEvent event){
//Remove Player from Wings' Fly Permission List
ItemWingsOfTheBats.removeWingsFromPlayer(event.player, true);
ItemWingsOfTheBats.removeWingsFromPlayer(event.player, false);
}
@SubscribeEvent
public void onEntityDropEvent(LivingDropsEvent event){
if(event.getEntityLiving().worldObj != null && !event.getEntityLiving().worldObj.isRemote && event.getSource().getEntity() instanceof EntityPlayer){
//Drop Wings from Bats
if(ConfigBoolValues.DO_BAT_DROPS.isEnabled() && event.getEntityLiving() instanceof EntityBat){
if(Util.RANDOM.nextInt(15) <= event.getLootingLevel()*2){
event.getEntityLiving().entityDropItem(new ItemStack(InitItems.itemMisc, Util.RANDOM.nextInt(2+event.getLootingLevel())+1, TheMiscItems.BAT_WING.ordinal()), 0);
}
}
}
}
@SubscribeEvent
public void onPlayerInteractEvent(PlayerInteractEvent event){
if(event.getEntityLiving() instanceof EntityPlayer){
EntityPlayer player = (EntityPlayer)event.getEntityLiving();
boolean wingsEquipped = ItemWingsOfTheBats.getWingItem(player) != null;
//If Player isn't (really) winged
if(!ItemWingsOfTheBats.isPlayerWinged(player)){
if(wingsEquipped){
//Make the Player actually winged
ItemWingsOfTheBats.addWingsToPlayer(player);
}
}
//If Player is (or should be) winged
else{
if(wingsEquipped){
//Allow the Player to fly when he has Wings equipped
player.capabilities.allowFlying = true;
}
else{
//Make the Player not winged
ItemWingsOfTheBats.removeWingsFromPlayer(player);
//Reset Player's Values
if(!player.capabilities.isCreativeMode){
player.capabilities.allowFlying = false;
player.capabilities.isFlying = false;
//Enables Fall Damage again (Automatically gets disabled for some reason)
player.capabilities.disableDamage = false;
}
}
}
}
2015-07-17 06:51:19 +02:00
}
/**
* Checks if the Player is winged
2015-10-02 16:48:01 +02:00
*
2015-07-17 06:51:19 +02:00
* @param player The Player
* @return Winged?
*/
public static boolean isPlayerWinged(EntityPlayer player){
2016-06-17 23:50:38 +02:00
return WINGED_PLAYERS.contains(player.getUniqueID()+(player.worldObj.isRemote ? "-Remote" : ""));
2015-07-17 06:51:19 +02:00
}
2015-10-03 10:19:40 +02:00
/**
* Same as above, but Remote Checking is done automatically
*/
public static void removeWingsFromPlayer(EntityPlayer player){
removeWingsFromPlayer(player, player.worldObj.isRemote);
}
2015-07-17 06:51:19 +02:00
/**
* Removes the Player from the List of Players that have Wings
2015-10-02 16:48:01 +02:00
*
* @param player The Player
2015-07-17 06:51:19 +02:00
* @param worldRemote If the World the Player is in is remote
*/
public static void removeWingsFromPlayer(EntityPlayer player, boolean worldRemote){
2016-06-17 23:50:38 +02:00
WINGED_PLAYERS.remove(player.getUniqueID()+(worldRemote ? "-Remote" : ""));
2015-07-17 06:51:19 +02:00
}
/**
* Adds the Player to the List of Players that have Wings
2015-10-02 16:48:01 +02:00
*
2015-07-17 06:51:19 +02:00
* @param player The Player
*/
public static void addWingsToPlayer(EntityPlayer player){
2016-06-17 23:50:38 +02:00
WINGED_PLAYERS.add(player.getUniqueID()+(player.worldObj.isRemote ? "-Remote" : ""));
2015-07-17 06:51:19 +02:00
}
2015-10-03 10:16:18 +02:00
/**
* Checks if the Player has Wings in its Inventory
*
* @param player The Player
* @return The Wings
*/
public static ItemStack getWingItem(EntityPlayer player){
for(int i = 0; i < player.inventory.getSizeInventory(); i++){
if(player.inventory.getStackInSlot(i) != null && player.inventory.getStackInSlot(i).getItem() instanceof ItemWingsOfTheBats){
return player.inventory.getStackInSlot(i);
}
}
return null;
}
2015-07-17 06:51:19 +02:00
@Override
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.EPIC;
2015-10-03 10:19:40 +02:00
}
2015-07-17 06:51:19 +02:00
}