Skip to content

Commit

Permalink
Add cooldown, when attacking mob, while invisible with Nuit's Vanishi…
Browse files Browse the repository at this point in the history
…ng. Closes #267
  • Loading branch information
GirafiStudios committed May 24, 2021
1 parent 4955e16 commit 7991fed
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void renderFog(EntityViewRenderEvent.RenderFogEvent event) {
public static void onRender(RenderPlayerEvent.Pre event) {
Optional<ImmutableTriple<String, Integer, ItemStack>> optional = CuriosApi.getCuriosHelper().findEquippedCurio(AtumItems.NUITS_VANISHING, event.getEntityLiving());
if (optional.isPresent()) {
if (!NuitsVanishingItem.isLivingEntityMoving(event.getEntityLiving())) {
if (!NuitsVanishingItem.TIMER.containsKey(event.getEntityLiving()) && !NuitsVanishingItem.isLivingEntityMoving(event.getEntityLiving())) {
event.setCanceled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import com.teammetallurgy.atum.items.artifacts.AmuletItem;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Effects;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand All @@ -22,6 +26,7 @@
@Mod.EventBusSubscriber(modid = Atum.MOD_ID)
public class NuitsVanishingItem extends AmuletItem implements IArtifact {
protected static final Object2BooleanMap<LivingEntity> INVISIBLE = new Object2BooleanOpenHashMap<>();
public static final Object2IntMap<LivingEntity> TIMER = new Object2IntOpenHashMap<>();

public NuitsVanishingItem() {
super(new Item.Properties().maxStackSize(1));
Expand All @@ -34,37 +39,62 @@ public God getGod() {

@SubscribeEvent
public static void onTarget(LivingSetAttackTargetEvent event) {
if (INVISIBLE.getBoolean(event.getTarget()) && event.getTarget() instanceof PlayerEntity && event.getEntityLiving() instanceof MobEntity) {
if (TIMER.getInt(event.getTarget()) <= 0 && INVISIBLE.getBoolean(event.getTarget()) && event.getTarget() instanceof PlayerEntity && event.getEntityLiving() instanceof MobEntity) {
((MobEntity) event.getEntityLiving()).setAttackTarget(null);
}
}

@SubscribeEvent
public static void onAttack(LivingAttackEvent event) {
Entity source = event.getSource().getTrueSource();
if (source instanceof LivingEntity) {
LivingEntity attacker = (LivingEntity) source;
if (INVISIBLE.getBoolean(attacker)) {
setNotInvisible(attacker);
TIMER.putIfAbsent(attacker, 200);
}
}
}

@Override
public void onUnequip(String identifier, int index, LivingEntity livingEntity, @Nonnull ItemStack stack) {
this.setNotInvisible(livingEntity);
setNotInvisible(livingEntity);
}

@Override
public void curioBreak(@Nonnull ItemStack stack, LivingEntity livingEntity) {
this.setNotInvisible(livingEntity);
setNotInvisible(livingEntity);
}

@Override
public void curioTick(String identifier, int index, LivingEntity livingEntity, @Nonnull ItemStack stack) {
World world = livingEntity.getEntityWorld();
INVISIBLE.putIfAbsent(livingEntity, true);
if (!TIMER.containsKey(livingEntity)) {
INVISIBLE.putIfAbsent(livingEntity, true);

if (!isLivingEntityMoving(livingEntity)) {
INVISIBLE.replace(livingEntity, true);
if (!world.isRemote) {
livingEntity.setInvisible(true);
}
} else {
setNotInvisible(livingEntity);
}
}

if (TIMER.containsKey(livingEntity)) {
int timer = TIMER.getInt(livingEntity);
if (timer == 0) {
TIMER.removeInt(livingEntity);
}

if (!isLivingEntityMoving(livingEntity)) {
INVISIBLE.replace(livingEntity, true);
if (!world.isRemote) {
livingEntity.setInvisible(true);
if (timer > 0) {
TIMER.replace(livingEntity, timer - 1);
}
} else {
this.setNotInvisible(livingEntity);
}
}

public void setNotInvisible(LivingEntity livingEntity) {
public static void setNotInvisible(LivingEntity livingEntity) {
INVISIBLE.replace(livingEntity, false);
if (!livingEntity.getEntityWorld().isRemote && !livingEntity.isPotionActive(Effects.INVISIBILITY) && livingEntity.isInvisible()) {
livingEntity.setInvisible(false);
Expand Down

0 comments on commit 7991fed

Please sign in to comment.