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

103 lines
3.8 KiB
Java
Raw Normal View History

2017-02-13 18:42:33 +01:00
/*
* This file ("ItemInfraredGoggles.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.items;
import java.util.List;
import java.util.Set;
2017-02-14 18:18:38 +01:00
import de.ellpeck.actuallyadditions.api.misc.IGoggles;
2017-02-13 18:42:33 +01:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.items.base.ItemArmorAA;
import de.ellpeck.actuallyadditions.mod.material.InitArmorMaterials;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2017-02-13 20:12:47 +01:00
import io.netty.util.internal.ConcurrentSet;
2017-02-13 18:42:33 +01:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2017-02-14 18:18:38 +01:00
public class ItemEngineerGoggles extends ItemArmorAA implements IGoggles{
2017-02-13 18:42:33 +01:00
2017-02-13 20:12:47 +01:00
private final Set<Entity> cachedGlowingEntities = new ConcurrentSet<Entity>();
2017-02-14 18:18:38 +01:00
private final boolean displayMobs;
public ItemEngineerGoggles(String name, boolean displayMobs){
2017-02-13 18:42:33 +01:00
super(name, InitArmorMaterials.armorMaterialGoggles, 0, StackUtil.getNull());
2017-02-14 18:18:38 +01:00
this.displayMobs = displayMobs;
2017-02-13 18:42:33 +01:00
this.setMaxDamage(0);
MinecraftForge.EVENT_BUS.register(this);
}
2017-04-01 19:40:39 +02:00
public static boolean isWearing(EntityPlayer player){
ItemStack face = player.inventory.armorInventory.get(3);
return StackUtil.isValid(face) && face.getItem() instanceof IGoggles;
}
2017-02-13 18:42:33 +01:00
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onClientTick(ClientTickEvent event){
EntityPlayer player = ActuallyAdditions.proxy.getCurrentPlayer();
if(player != null && isWearing(player)){
2017-02-14 18:18:38 +01:00
ItemStack face = player.inventory.armorInventory.get(3);
if(((IGoggles)face.getItem()).displaySpectralMobs()){
double range = 8;
AxisAlignedBB aabb = new AxisAlignedBB(player.posX-range, player.posY-range, player.posZ-range, player.posX+range, player.posY+range, player.posZ+range);
List<Entity> entities = player.world.getEntitiesWithinAABB(Entity.class, aabb);
if(entities != null && !entities.isEmpty()){
this.cachedGlowingEntities.addAll(entities);
}
2017-02-13 18:42:33 +01:00
2017-02-14 18:18:38 +01:00
if(!this.cachedGlowingEntities.isEmpty()){
for(Entity entity : this.cachedGlowingEntities){
if(entity.isDead || entity.getDistanceSq(player.posX, player.posY, player.posZ) > range*range){
entity.setGlowing(false);
2017-02-13 20:12:47 +01:00
2017-02-14 18:18:38 +01:00
this.cachedGlowingEntities.remove(entity);
}
else{
entity.setGlowing(true);
}
2017-02-13 20:12:47 +01:00
}
}
2017-02-14 18:18:38 +01:00
return;
2017-02-13 20:12:47 +01:00
}
}
2017-02-14 18:18:38 +01:00
if(!this.cachedGlowingEntities.isEmpty()){
for(Entity entity : this.cachedGlowingEntities){
if(!entity.isDead){
entity.setGlowing(false);
2017-02-13 18:42:33 +01:00
}
}
2017-02-14 18:18:38 +01:00
this.cachedGlowingEntities.clear();
2017-02-13 18:42:33 +01:00
}
}
@Override
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.RARE;
}
2017-02-14 18:18:38 +01:00
@Override
public boolean displaySpectralMobs(){
return this.displayMobs;
}
2017-02-13 18:42:33 +01:00
}