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
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 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-11-22 18:26:29 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
|
2021-05-04 18:47:45 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.proxy.ClientProxy;
|
2016-11-16 20:31:16 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2021-03-01 21:23:52 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
2016-12-31 16:15:56 +01:00
|
|
|
import net.minecraft.entity.Entity;
|
2021-05-04 18:47:45 +02:00
|
|
|
import net.minecraft.entity.LivingEntity;
|
2021-03-01 21:30:45 +01:00
|
|
|
import net.minecraft.entity.item.ItemEntity;
|
2021-05-04 18:47:45 +02:00
|
|
|
import net.minecraft.entity.passive.BatEntity;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2015-07-17 06:51:19 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-05-04 18:47:45 +02:00
|
|
|
import net.minecraft.item.SwordItem;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2016-11-22 19:35:52 +01:00
|
|
|
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;
|
2021-05-04 18:47:45 +02:00
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
2015-07-17 06:51:19 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class ItemWingsOfTheBats extends ItemBase {
|
2015-07-17 06:51:19 +02:00
|
|
|
|
2016-12-31 16:15:56 +01:00
|
|
|
public static final String THE_BAT_BAT = "the bat bat";
|
2016-11-22 19:35:52 +01:00
|
|
|
public static final int MAX_FLY_TIME = 800;
|
2015-07-17 06:51:19 +02:00
|
|
|
|
2021-03-01 21:23:52 +01:00
|
|
|
public ItemWingsOfTheBats() {
|
2021-08-22 17:09:06 +02:00
|
|
|
super(ActuallyItems.defaultProps().stacksTo(1));
|
2016-07-03 20:57:00 +02:00
|
|
|
|
2021-05-04 18:47:45 +02:00
|
|
|
// TODO: Lets move this somewhere global. Don't like event logic in a single place.
|
2021-12-30 18:30:01 +01:00
|
|
|
//MinecraftForge.EVENT_BUS.register(this);
|
2016-07-03 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
/**
|
|
|
|
* Checks if the Player has Wings in its Inventory
|
|
|
|
*
|
|
|
|
* @param player The Player
|
2021-02-27 16:33:00 +01:00
|
|
|
*
|
2016-11-26 21:32:27 +01:00
|
|
|
* @return The Wings
|
|
|
|
*/
|
2021-02-26 22:15:48 +01:00
|
|
|
public static ItemStack getWingItem(PlayerEntity player) {
|
2021-08-22 17:09:06 +02:00
|
|
|
for (int i = 0; i < player.inventory.getContainerSize(); i++) {
|
|
|
|
if (StackUtil.isValid(player.inventory.getItem(i)) && player.inventory.getItem(i).getItem() instanceof ItemWingsOfTheBats) {
|
|
|
|
return player.inventory.getItem(i);
|
2021-02-27 16:33:00 +01:00
|
|
|
}
|
2016-11-26 21:32:27 +01:00
|
|
|
}
|
2017-11-02 22:49:53 +01:00
|
|
|
return StackUtil.getEmpty();
|
2016-11-26 21:32:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-22 18:26:29 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean showDurabilityBar(ItemStack stack) {
|
2016-11-22 18:26:29 +01:00
|
|
|
return true;
|
2016-07-07 17:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 18:26:29 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public double getDurabilityForDisplay(ItemStack stack) {
|
2021-12-30 18:30:01 +01:00
|
|
|
/* PlayerEntity player = ClientProxy.getCurrentPlayer();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (player != null) {
|
2021-11-21 22:21:07 +01:00
|
|
|
// PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
|
|
|
|
double diff = MAX_FLY_TIME - 1;//data.batWingsFlyTime; // TODO: fix me
|
2021-05-04 18:47:45 +02:00
|
|
|
return 1 - diff / MAX_FLY_TIME;
|
2021-12-30 18:30:01 +01:00
|
|
|
}*/ //TODO
|
2016-11-27 21:53:16 +01:00
|
|
|
return super.getDurabilityForDisplay(stack);
|
2016-07-07 17:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 18:26:29 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getRGBDurabilityForDisplay(ItemStack stack) {
|
2021-12-30 18:30:01 +01:00
|
|
|
/* PlayerEntity player = ClientProxy.getCurrentPlayer();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (player != null) {
|
2021-11-21 22:21:07 +01:00
|
|
|
// PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
|
|
|
|
int curr = 1;//data.batWingsFlyTime; // TODO: fix me
|
2021-08-22 17:09:06 +02:00
|
|
|
return MathHelper.hsvToRgb(Math.max(0.0F, 1 - (float) curr / MAX_FLY_TIME) / 3.0F, 1.0F, 1.0F);
|
2021-12-30 18:30:01 +01:00
|
|
|
}*/
|
2016-11-27 21:53:16 +01:00
|
|
|
return super.getRGBDurabilityForDisplay(stack);
|
2016-07-07 17:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:57:00 +02:00
|
|
|
@SubscribeEvent
|
2019-05-02 09:10:29 +02:00
|
|
|
public void onEntityDropEvent(LivingDropsEvent event) {
|
2021-08-22 17:09:06 +02:00
|
|
|
Entity source = event.getSource().getEntity();
|
2016-12-31 16:15:56 +01:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (event.getEntityLiving().level != null && !event.getEntityLiving().level.isClientSide && source instanceof PlayerEntity) {
|
2016-07-03 20:57:00 +02:00
|
|
|
//Drop Wings from Bats
|
2021-05-04 18:47:45 +02:00
|
|
|
if (ConfigBoolValues.DO_BAT_DROPS.isEnabled() && event.getEntityLiving() instanceof BatEntity) {
|
2016-12-31 16:15:56 +01:00
|
|
|
int looting = event.getLootingLevel();
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
Iterable<ItemStack> equip = source.getHandSlots();
|
2019-05-02 09:10:29 +02:00
|
|
|
for (ItemStack stack : equip) {
|
2021-05-04 18:47:45 +02:00
|
|
|
// Todo: [port] this might not work anymore due to the way things are checked
|
2021-08-22 17:09:06 +02:00
|
|
|
if (StackUtil.isValid(stack) && ItemWingsOfTheBats.THE_BAT_BAT.equalsIgnoreCase(stack.getHoverName().getString()) && stack.getItem() instanceof SwordItem) {
|
2016-12-31 16:15:56 +01:00
|
|
|
looting += 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (event.getEntityLiving().level.random.nextInt(15) <= looting * 2) {
|
2021-05-04 18:47:45 +02:00
|
|
|
LivingEntity entityLiving = event.getEntityLiving();
|
2021-11-24 17:57:31 +01:00
|
|
|
event.getDrops().add(new ItemEntity(event.getEntityLiving().level, entityLiving.getX(), entityLiving.getY(), entityLiving.getZ(), new ItemStack(ActuallyItems.BATS_WING.get(), event.getEntityLiving().level.random.nextInt(2 + looting) + 1)));
|
2016-07-03 20:57:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2019-05-02 09:10:29 +02:00
|
|
|
public void livingUpdateEvent(LivingEvent.LivingUpdateEvent event) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (event.getEntityLiving() instanceof PlayerEntity) {
|
|
|
|
PlayerEntity player = (PlayerEntity) event.getEntityLiving();
|
2016-07-03 20:57:00 +02:00
|
|
|
|
2021-11-24 19:19:31 +01:00
|
|
|
if (false &&!player.isCreative() && !player.isSpectator()) { //TODO disabled for now.
|
2016-11-22 18:26:29 +01:00
|
|
|
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!player.level.isClientSide) {
|
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));
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!data.hasBatWings) {
|
|
|
|
if (data.batWingsFlyTime <= 0) {
|
|
|
|
if (wingsEquipped) {
|
2016-11-22 18:26:29 +01:00
|
|
|
data.hasBatWings = true;
|
|
|
|
shouldSend = true;
|
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-11-22 19:35:52 +01:00
|
|
|
tryDeduct = true;
|
2016-11-22 18:26:29 +01:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
|
|
|
if (wingsEquipped && data.batWingsFlyTime < MAX_FLY_TIME) {
|
2021-08-22 17:09:06 +02:00
|
|
|
player.abilities.mayfly = true;
|
2016-11-22 18:26:29 +01:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (player.abilities.flying) {
|
2016-11-22 18:26:29 +01:00
|
|
|
data.batWingsFlyTime++;
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (player.level.getLevelData().getGameTime() % 10 == 0) {
|
2016-11-22 18:26:29 +01:00
|
|
|
shouldSend = true;
|
|
|
|
}
|
|
|
|
}
|
2016-11-22 19:35:52 +01:00
|
|
|
|
|
|
|
tryDeduct = true;
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-11-22 18:26:29 +01:00
|
|
|
data.hasBatWings = false;
|
2016-11-22 19:35:52 +01:00
|
|
|
data.shouldDisableBatWings = true;
|
2016-11-22 18:26:29 +01:00
|
|
|
shouldSend = true;
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
player.abilities.mayfly = false;
|
|
|
|
player.abilities.flying = false;
|
|
|
|
player.abilities.invulnerable = false;
|
2016-11-22 18:26:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tryDeduct && data.batWingsFlyTime > 0) {
|
2016-11-22 19:35:52 +01:00
|
|
|
int deductTime = 0;
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!player.abilities.flying) {
|
2016-11-22 19:35:52 +01:00
|
|
|
deductTime = 2;
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockPos pos = new BlockPos(player.getX(), player.getY() + player.getBbHeight(), player.getZ());
|
|
|
|
BlockState state = player.level.getBlockState(pos);
|
|
|
|
if (state.isFaceSturdy(player.level, pos, Direction.DOWN)) {
|
2016-11-22 19:35:52 +01:00
|
|
|
deductTime = 10;
|
|
|
|
}
|
2016-11-22 18:26:29 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (deductTime > 0) {
|
|
|
|
data.batWingsFlyTime = Math.max(0, data.batWingsFlyTime - deductTime);
|
2016-11-22 19:35:52 +01:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (player.level.getLevelData().getGameTime() % 10 == 0) {
|
2016-11-22 19:35:52 +01:00
|
|
|
shouldSend = true;
|
|
|
|
}
|
2016-11-22 18:26:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (shouldSend) {
|
2017-02-13 15:23:28 +01:00
|
|
|
PacketHandlerHelper.syncPlayerData(player, false);
|
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
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
|
|
|
if (data.hasBatWings) {
|
2021-08-22 17:09:06 +02:00
|
|
|
player.abilities.mayfly = true;
|
2019-05-02 09:10:29 +02:00
|
|
|
} else if (data.shouldDisableBatWings) { //so that other modded flying won't be disabled
|
2016-11-22 19:35:52 +01:00
|
|
|
data.shouldDisableBatWings = false;
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
player.abilities.mayfly = false;
|
|
|
|
player.abilities.flying = false;
|
|
|
|
player.abilities.invulnerable = false;
|
2016-07-03 20:57:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 06:51:19 +02:00
|
|
|
}
|
|
|
|
}
|