NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemShockwaveCreator.java

141 lines
6.1 KiB
Java
Raw Normal View History

2018-11-06 19:02:56 +01:00
package de.ellpeck.naturesaura.items;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.render.ITrinketItem;
2018-12-01 18:56:05 +01:00
import de.ellpeck.naturesaura.items.tools.ItemArmorNA;
2018-11-06 19:02:56 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
2018-11-06 19:02:56 +01:00
import net.minecraft.client.Minecraft;
2019-10-20 22:30:49 +02:00
import net.minecraft.client.gui.AbstractGui;
import com.mojang.blaze3d.platform.GlStateManager;
2018-11-06 19:02:56 +01:00
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.entity.Entity;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.potion.Effects;
import net.minecraft.inventory.EquipmentSlotType;
2018-11-06 19:02:56 +01:00
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.potion.EffectInstance;
2018-11-06 19:02:56 +01:00
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2019-10-20 22:30:49 +02:00
import net.minecraft.world.ServerWorld;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2018-11-06 19:02:56 +01:00
import java.util.List;
public class ItemShockwaveCreator extends ItemImpl implements ITrinketItem {
2018-11-20 11:48:45 +01:00
private static final ResourceLocation RES_WORN = new ResourceLocation(NaturesAura.MOD_ID, "textures/items/shockwave_creator_player.png");
2018-11-06 19:02:56 +01:00
public ItemShockwaveCreator() {
super("shockwave_creator");
this.setMaxStackSize(1);
}
@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
2019-10-20 22:30:49 +02:00
if (worldIn.isRemote || !(entityIn instanceof LivingEntity))
2018-11-06 19:02:56 +01:00
return;
2019-10-20 22:30:49 +02:00
LivingEntity living = (LivingEntity) entityIn;
2018-11-06 19:02:56 +01:00
if (!living.onGround) {
if (!stack.hasTagCompound())
2019-10-20 22:30:49 +02:00
stack.setTagCompound(new CompoundNBT());
CompoundNBT compound = stack.getTagCompound();
2018-11-06 19:02:56 +01:00
if (compound.getBoolean("air"))
return;
compound.setBoolean("air", true);
compound.setDouble("x", living.posX);
compound.setDouble("y", living.posY);
compound.setDouble("z", living.posZ);
} else {
if (!stack.hasTagCompound())
return;
2019-10-20 22:30:49 +02:00
CompoundNBT compound = stack.getTagCompound();
2018-11-06 19:02:56 +01:00
if (!compound.getBoolean("air"))
return;
compound.setBoolean("air", false);
if (!living.isSneaking())
return;
if (living.getDistanceSq(compound.getDouble("x"), compound.getDouble("y"), compound.getDouble("z")) > 0.75F)
return;
2019-10-20 22:30:49 +02:00
if (living instanceof PlayerEntity && !NaturesAuraAPI.instance().extractAuraFromPlayer((PlayerEntity) living, 1000, false))
2018-11-06 19:02:56 +01:00
return;
2018-11-06 23:08:42 +01:00
DamageSource source;
2019-10-20 22:30:49 +02:00
if (living instanceof PlayerEntity)
source = DamageSource.causePlayerDamage((PlayerEntity) living);
2018-11-06 23:08:42 +01:00
else
source = DamageSource.MAGIC;
2018-12-01 18:56:05 +01:00
boolean infusedSet = ItemArmorNA.isFullSetEquipped(living, 0);
2018-11-06 23:08:42 +01:00
2018-11-06 19:02:56 +01:00
int range = 5;
2019-10-20 22:30:49 +02:00
List<LivingEntity> mobs = worldIn.getEntitiesWithinAABB(LivingEntity.class, new AxisAlignedBB(
2018-11-06 19:02:56 +01:00
living.posX - range, living.posY - 0.5, living.posZ - range,
living.posX + range, living.posY + 0.5, living.posZ + range));
2019-10-20 22:30:49 +02:00
for (LivingEntity mob : mobs) {
2018-11-06 19:02:56 +01:00
if (mob.isDead || mob == living)
continue;
if (living.getDistanceSq(mob) > range * range)
continue;
2019-10-20 22:30:49 +02:00
if (living instanceof PlayerEntity && !NaturesAuraAPI.instance().extractAuraFromPlayer((PlayerEntity) living, 500, false))
2018-11-06 19:02:56 +01:00
break;
2018-11-06 23:08:42 +01:00
mob.attackEntityFrom(source, 4F);
2018-12-01 18:56:05 +01:00
if (infusedSet)
2019-10-20 22:30:49 +02:00
mob.addPotionEffect(new EffectInstance(Effects.WITHER, 120));
2018-11-06 19:02:56 +01:00
}
BlockPos pos = living.getPosition();
BlockPos down = pos.down();
2019-10-20 22:30:49 +02:00
BlockState downState = worldIn.getBlockState(down);
2018-11-06 19:02:56 +01:00
if (downState.getMaterial() != Material.AIR) {
SoundType type = downState.getBlock().getSoundType(downState, worldIn, down, null);
worldIn.playSound(null, pos, type.getBreakSound(), SoundCategory.BLOCKS, type.getVolume() * 0.5F, type.getPitch() * 0.8F);
}
2019-10-20 22:30:49 +02:00
if (worldIn instanceof ServerWorld)
((ServerWorld) worldIn).spawnParticle(EnumParticleTypes.BLOCK_DUST,
2018-11-06 19:02:56 +01:00
living.posX, living.posY + 0.01F, living.posZ,
15, 0F, 0F, 0F, 0.15F, Block.getStateId(downState));
PacketHandler.sendToAllAround(worldIn, pos, 32,
new PacketParticles((float) living.posX, (float) living.posY, (float) living.posZ, 11));
}
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
public void render(ItemStack stack, PlayerEntity player, RenderType type, boolean isHolding) {
if (type == RenderType.BODY && !isHolding) {
2019-10-20 22:30:49 +02:00
boolean armor = !player.inventory.armorInventory.get(EquipmentSlotType.CHEST.getIndex()).isEmpty();
2018-11-07 23:57:47 +01:00
GlStateManager.translate(-0.1675F, -0.05F, armor ? -0.195F : -0.1475F);
2018-11-06 19:02:56 +01:00
GlStateManager.scale(0.021F, 0.021F, 0.021F);
GlStateManager.pushMatrix();
GlStateManager.disableLighting();
GlStateManager.pushAttrib();
RenderHelper.enableStandardItemLighting();
Minecraft.getMinecraft().getTextureManager().bindTexture(RES_WORN);
2019-10-20 22:30:49 +02:00
AbstractGui.drawModalRectWithCustomSizedTexture(0, 0, 0, 0, 16, 16, 16, 16);
2018-11-06 19:02:56 +01:00
RenderHelper.disableStandardItemLighting();
GlStateManager.popAttrib();
GlStateManager.enableLighting();
GlStateManager.popMatrix();
}
}
}