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

135 lines
5.9 KiB
Java
Raw Normal View History

2018-11-06 19:02:56 +01:00
package de.ellpeck.naturesaura.items;
2019-11-04 19:08:49 +01:00
import com.mojang.blaze3d.platform.GlStateManager;
2018-11-06 19:02:56 +01:00
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.render.ITrinketItem;
2019-11-04 19:08:49 +01:00
import de.ellpeck.naturesaura.items.tools.Armor;
import net.minecraft.block.BlockState;
2018-11-06 19:02:56 +01:00
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
2019-11-04 19:08:49 +01:00
import net.minecraft.client.gui.screen.Screen;
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.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;
2019-11-04 19:08:49 +01:00
import net.minecraft.particles.ParticleTypes;
2019-10-20 22:30:49 +02:00
import net.minecraft.potion.EffectInstance;
2019-11-04 19:08:49 +01:00
import net.minecraft.potion.Effects;
2018-11-06 19:02:56 +01:00
import net.minecraft.util.DamageSource;
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-11-04 19:08:49 +01:00
import net.minecraft.world.server.ServerWorld;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2018-11-06 19:02:56 +01:00
import java.util.List;
2019-11-04 19:08:49 +01:00
public class ShockwaveCreator extends ItemImpl implements ITrinketItem {
2018-11-06 19:02:56 +01:00
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
2019-11-04 19:08:49 +01:00
public ShockwaveCreator() {
super("shockwave_creator", new Properties().maxStackSize(1).group(NaturesAura.CREATIVE_TAB));
2018-11-06 19:02:56 +01:00
}
@Override
2019-11-04 19:08:49 +01:00
public void inventoryTick(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) {
2019-11-04 19:08:49 +01:00
CompoundNBT compound = stack.getOrCreateTag();
2018-11-06 19:02:56 +01:00
if (compound.getBoolean("air"))
return;
2019-11-04 19:08:49 +01:00
compound.putBoolean("air", true);
compound.putDouble("x", living.posX);
compound.putDouble("y", living.posY);
compound.putDouble("z", living.posZ);
2018-11-06 19:02:56 +01:00
} else {
2019-11-04 19:08:49 +01:00
if (!stack.hasTag())
2018-11-06 19:02:56 +01:00
return;
2019-11-04 19:08:49 +01:00
CompoundNBT compound = stack.getTag();
2018-11-06 19:02:56 +01:00
if (!compound.getBoolean("air"))
return;
2019-11-04 19:08:49 +01:00
compound.putBoolean("air", false);
2018-11-06 19:02:56 +01:00
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;
2019-11-04 19:08:49 +01:00
boolean infusedSet = Armor.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) {
2019-11-04 19:08:49 +01:00
if (!mob.isAlive() || mob == living)
2018-11-06 19:02:56 +01:00
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)
2019-11-04 19:08:49 +01:00
((ServerWorld) worldIn).spawnParticle(ParticleTypes.POOF, living.posX, living.posY + 0.01F, living.posZ, 15, 0D, 0D, 0D, 0.15D);
// TODO PacketHandler.sendToAllAround(worldIn, pos, 32, new PacketParticles((float) living.posX, (float) living.posY, (float) living.posZ, 11));
2018-11-06 19:02:56 +01:00
}
}
@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();
2019-11-04 19:08:49 +01:00
GlStateManager.translatef(-0.1675F, -0.05F, armor ? -0.195F : -0.1475F);
GlStateManager.scalef(0.021F, 0.021F, 0.021F);
2018-11-06 19:02:56 +01:00
GlStateManager.pushMatrix();
GlStateManager.disableLighting();
2019-11-04 19:08:49 +01:00
GlStateManager.pushTextureAttributes();
GlStateManager.pushLightingAttributes();
2018-11-06 19:02:56 +01:00
RenderHelper.enableStandardItemLighting();
2019-11-04 19:08:49 +01:00
Minecraft.getInstance().getTextureManager().bindTexture(RES_WORN);
Screen.blit(0, 0, 0, 0, 16, 16, 16, 16);
2018-11-06 19:02:56 +01:00
RenderHelper.disableStandardItemLighting();
2019-11-04 19:08:49 +01:00
GlStateManager.popAttributes();
GlStateManager.popAttributes();
2018-11-06 19:02:56 +01:00
GlStateManager.enableLighting();
GlStateManager.popMatrix();
}
}
}