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-11-22 18:26:29 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
|
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;
|
2016-11-22 18:26:29 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
|
2016-11-16 20:31:16 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-11-22 19:35:52 +01:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2016-11-22 18:26:29 +01:00
|
|
|
import net.minecraft.client.Minecraft;
|
2016-07-03 20:57:00 +02:00
|
|
|
import net.minecraft.entity.passive.EntityBat;
|
2015-07-17 06:51:19 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.EnumRarity;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2016-11-22 19:35:52 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-11-22 18:26:29 +01:00
|
|
|
import net.minecraft.util.math.MathHelper;
|
2016-07-03 20:57:00 +02:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.entity.living.LivingDropsEvent;
|
2016-07-03 21:06:44 +02:00
|
|
|
import net.minecraftforge.event.entity.living.LivingEvent;
|
2016-07-03 20:57:00 +02:00
|
|
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
2016-11-22 18:26:29 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2015-07-17 06:51:19 +02:00
|
|
|
|
2015-12-03 20:15:07 +01:00
|
|
|
public class ItemWingsOfTheBats extends ItemBase{
|
2015-07-17 06:51:19 +02:00
|
|
|
|
2016-11-22 19:35:52 +01:00
|
|
|
public static final int MAX_FLY_TIME = 800;
|
2015-07-17 06:51:19 +02:00
|
|
|
|
2015-12-03 20:15:07 +01: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);
|
|
|
|
}
|
|
|
|
|
2016-11-26 21:32:27 +01: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(StackUtil.isValid(player.inventory.getStackInSlot(i)) && player.inventory.getStackInSlot(i).getItem() instanceof ItemWingsOfTheBats){
|
|
|
|
return player.inventory.getStackInSlot(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return StackUtil.getNull();
|
|
|
|
}
|
|
|
|
|
2016-11-22 18:26:29 +01:00
|
|
|
@Override
|
|
|
|
public boolean showDurabilityBar(ItemStack stack){
|
|
|
|
return true;
|
2016-07-07 17:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 18:26:29 +01:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public double getDurabilityForDisplay(ItemStack stack){
|
2016-11-26 21:32:27 +01:00
|
|
|
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(Minecraft.getMinecraft().player);
|
2016-11-22 18:26:29 +01:00
|
|
|
if(data != null){
|
|
|
|
double diff = MAX_FLY_TIME-data.batWingsFlyTime;
|
|
|
|
return 1-(diff/MAX_FLY_TIME);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return super.getDurabilityForDisplay(stack);
|
|
|
|
}
|
2016-07-07 17:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 18:26:29 +01:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getRGBDurabilityForDisplay(ItemStack stack){
|
2016-11-26 21:32:27 +01:00
|
|
|
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(Minecraft.getMinecraft().player);
|
2016-11-22 18:26:29 +01:00
|
|
|
if(data != null){
|
|
|
|
int curr = data.batWingsFlyTime;
|
|
|
|
return MathHelper.hsvToRGB(Math.max(0.0F, 1-(float)curr/MAX_FLY_TIME)/3.0F, 1.0F, 1.0F);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return super.getRGBDurabilityForDisplay(stack);
|
|
|
|
}
|
2016-07-07 17:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:57:00 +02:00
|
|
|
@SubscribeEvent
|
|
|
|
public void onEntityDropEvent(LivingDropsEvent event){
|
2016-11-26 21:32:27 +01:00
|
|
|
if(event.getEntityLiving().world != null && !event.getEntityLiving().world.isRemote && event.getSource().getEntity() instanceof EntityPlayer){
|
2016-07-03 20:57:00 +02:00
|
|
|
//Drop Wings from Bats
|
|
|
|
if(ConfigBoolValues.DO_BAT_DROPS.isEnabled() && event.getEntityLiving() instanceof EntityBat){
|
2016-11-26 21:32:27 +01:00
|
|
|
if(event.getEntityLiving().world.rand.nextInt(15) <= event.getLootingLevel()*2){
|
|
|
|
event.getEntityLiving().entityDropItem(new ItemStack(InitItems.itemMisc, event.getEntityLiving().world.rand.nextInt(2+event.getLootingLevel())+1, TheMiscItems.BAT_WING.ordinal()), 0);
|
2016-07-03 20:57:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2016-07-03 21:06:44 +02:00
|
|
|
public void livingUpdateEvent(LivingEvent.LivingUpdateEvent event){
|
2016-07-03 20:57:00 +02:00
|
|
|
if(event.getEntityLiving() instanceof EntityPlayer){
|
|
|
|
EntityPlayer player = (EntityPlayer)event.getEntityLiving();
|
|
|
|
|
2016-11-22 18:26:29 +01:00
|
|
|
if(!player.capabilities.isCreativeMode){
|
|
|
|
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
|
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
if(!player.world.isRemote){
|
2016-11-22 19:35:52 +01:00
|
|
|
boolean tryDeduct = false;
|
2016-11-22 18:26:29 +01:00
|
|
|
boolean shouldSend = false;
|
|
|
|
|
|
|
|
boolean wingsEquipped = StackUtil.isValid(ItemWingsOfTheBats.getWingItem(player));
|
|
|
|
if(!data.hasBatWings){
|
|
|
|
if(data.batWingsFlyTime <= 0){
|
|
|
|
if(wingsEquipped){
|
|
|
|
data.hasBatWings = true;
|
|
|
|
shouldSend = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2016-11-22 19:35:52 +01:00
|
|
|
tryDeduct = true;
|
2016-11-22 18:26:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if(wingsEquipped && data.batWingsFlyTime < MAX_FLY_TIME){
|
|
|
|
player.capabilities.allowFlying = true;
|
|
|
|
|
|
|
|
if(player.capabilities.isFlying){
|
|
|
|
data.batWingsFlyTime++;
|
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
if(player.world.getTotalWorldTime()%10 == 0){
|
2016-11-22 18:26:29 +01:00
|
|
|
shouldSend = true;
|
|
|
|
}
|
|
|
|
}
|
2016-11-22 19:35:52 +01:00
|
|
|
|
|
|
|
tryDeduct = true;
|
2016-11-22 18:26:29 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
data.hasBatWings = false;
|
2016-11-22 19:35:52 +01:00
|
|
|
data.shouldDisableBatWings = true;
|
2016-11-22 18:26:29 +01:00
|
|
|
shouldSend = true;
|
|
|
|
|
|
|
|
player.capabilities.allowFlying = false;
|
|
|
|
player.capabilities.isFlying = false;
|
|
|
|
player.capabilities.disableDamage = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:35:52 +01:00
|
|
|
if(tryDeduct && data.batWingsFlyTime > 0){
|
|
|
|
int deductTime = 0;
|
|
|
|
|
|
|
|
if(!player.capabilities.isFlying){
|
|
|
|
deductTime = 2;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
BlockPos pos = new BlockPos(player.posX, player.posY+player.height, player.posZ);
|
2016-11-26 21:32:27 +01:00
|
|
|
IBlockState state = player.world.getBlockState(pos);
|
|
|
|
if(state != null && state.isSideSolid(player.world, pos, EnumFacing.DOWN)){
|
2016-11-22 19:35:52 +01:00
|
|
|
deductTime = 10;
|
|
|
|
}
|
2016-11-22 18:26:29 +01:00
|
|
|
}
|
|
|
|
|
2016-11-22 19:35:52 +01:00
|
|
|
if(deductTime > 0){
|
|
|
|
data.batWingsFlyTime = Math.max(0, data.batWingsFlyTime-deductTime);
|
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
if(player.world.getTotalWorldTime()%10 == 0){
|
2016-11-22 19:35:52 +01:00
|
|
|
shouldSend = true;
|
|
|
|
}
|
2016-11-22 18:26:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(shouldSend){
|
|
|
|
PacketHandlerHelper.sendPlayerDataPacket(player, false, true);
|
2016-11-22 19:35:52 +01:00
|
|
|
data.shouldDisableBatWings = false; //was set only temporarily to send it
|
2016-11-22 18:26:29 +01:00
|
|
|
}
|
2016-07-03 20:57:00 +02:00
|
|
|
}
|
|
|
|
else{
|
2016-11-22 18:26:29 +01:00
|
|
|
if(data.hasBatWings){
|
|
|
|
player.capabilities.allowFlying = true;
|
|
|
|
}
|
2016-11-22 19:35:52 +01:00
|
|
|
else if(data.shouldDisableBatWings){ //so that other modded flying won't be disabled
|
|
|
|
data.shouldDisableBatWings = false;
|
|
|
|
|
2016-07-03 20:57:00 +02:00
|
|
|
player.capabilities.allowFlying = false;
|
|
|
|
player.capabilities.isFlying = false;
|
|
|
|
player.capabilities.disableDamage = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 06:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumRarity getRarity(ItemStack stack){
|
2016-01-07 21:41:28 +01:00
|
|
|
return EnumRarity.EPIC;
|
2015-10-03 10:19:40 +02:00
|
|
|
}
|
2015-07-17 06:51:19 +02:00
|
|
|
}
|