From 4ba567d5e529c5bfaae08dfe77a3afa15fd38947 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 13 Feb 2019 22:43:08 +0200 Subject: [PATCH] Added caster utils to easily find magically enabled entities in the world, and make it easier to get out an effect of a given type --- .../unicopia/entity/EntitySpell.java | 6 +- .../unicopia/network/EffectSync.java | 15 +++- .../unicopia/player/PlayerCapabilities.java | 4 +- .../unicopia/render/DisguiseRenderer.java | 7 +- .../unicopia/spell/CasterUtils.java | 69 +++++++++++++++++++ .../unicopia/spell/ICaster.java | 25 ++++++- .../minelittlepony/util/vector/VecHelper.java | 2 +- 7 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/spell/CasterUtils.java diff --git a/src/main/java/com/minelittlepony/unicopia/entity/EntitySpell.java b/src/main/java/com/minelittlepony/unicopia/entity/EntitySpell.java index 275be2d9..4fb4ccf1 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/EntitySpell.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/EntitySpell.java @@ -97,9 +97,9 @@ public class EntitySpell extends EntityLiving implements IMagicals, ICaster T getEffect(@Nullable Class type, boolean update) { + return effectDelegate.get(type, update); + } @Override public boolean hasEffect() { diff --git a/src/main/java/com/minelittlepony/unicopia/network/EffectSync.java b/src/main/java/com/minelittlepony/unicopia/network/EffectSync.java index da15b058..1f2bc49d 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/EffectSync.java +++ b/src/main/java/com/minelittlepony/unicopia/network/EffectSync.java @@ -55,9 +55,14 @@ public class EffectSync { return effect != null; } - public IMagicEffect get(boolean update) { + @SuppressWarnings("unchecked") + public E get(Class type, boolean update) { if (!update) { - return effect; + if (effect == null || type == null || type.isAssignableFrom(effect.getClass())) { + return (E)effect; + } + + return null; } NBTTagCompound comp = owned.getEntity().getDataManager().get(param); @@ -82,7 +87,11 @@ public class EffectSync { } } - return effect; + if (effect == null || type == null || type.isAssignableFrom(effect.getClass())) { + return (E)effect; + } + + return null; } public void set(@Nullable IMagicEffect effect) { diff --git a/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java b/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java index b551f908..4c698b33 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/player/PlayerCapabilities.java @@ -369,8 +369,8 @@ class PlayerCapabilities implements IPlayer { @Nullable @Override - public IMagicEffect getEffect(boolean update) { - return effectDelegate.get(update); + public T getEffect(@Nullable Class type, boolean update) { + return effectDelegate.get(type, update); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/render/DisguiseRenderer.java b/src/main/java/com/minelittlepony/unicopia/render/DisguiseRenderer.java index 506d2c01..01b96754 100644 --- a/src/main/java/com/minelittlepony/unicopia/render/DisguiseRenderer.java +++ b/src/main/java/com/minelittlepony/unicopia/render/DisguiseRenderer.java @@ -1,7 +1,6 @@ package com.minelittlepony.unicopia.render; import com.minelittlepony.unicopia.player.IPlayer; -import com.minelittlepony.unicopia.spell.IMagicEffect; import com.minelittlepony.unicopia.spell.SpellDisguise; import net.minecraft.client.Minecraft; @@ -50,9 +49,9 @@ public class DisguiseRenderer { } public boolean renderDisguiseToGui(IPlayer player) { - IMagicEffect effect = player.getEffect(false); + SpellDisguise effect = player.getEffect(SpellDisguise.class, false); - if (!(effect instanceof SpellDisguise) || effect.getDead()) { + if (effect == null || effect.getDead()) { return false; } @@ -63,7 +62,7 @@ public class DisguiseRenderer { return false; } - Entity e = ((SpellDisguise)effect).getDisguise(); + Entity e = effect.getDisguise(); // Check for a disguise and render it in our place. if (e != null) { diff --git a/src/main/java/com/minelittlepony/unicopia/spell/CasterUtils.java b/src/main/java/com/minelittlepony/unicopia/spell/CasterUtils.java new file mode 100644 index 00000000..53381970 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/spell/CasterUtils.java @@ -0,0 +1,69 @@ +package com.minelittlepony.unicopia.spell; + +import java.util.Optional; +import java.util.stream.Stream; + +import javax.annotation.Nullable; + +import com.minelittlepony.unicopia.Predicates; +import com.minelittlepony.unicopia.player.PlayerSpeciesList; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.math.AxisAlignedBB; +import net.minecraft.util.math.BlockPos; + +public class CasterUtils { + + public static Stream> findAllSpellsInRange(ICaster source, double radius) { + + BlockPos origin = source.getOrigin(); + + BlockPos begin = origin.add(-radius, -radius, -radius); + BlockPos end = origin.add(radius, radius, radius); + + AxisAlignedBB bb = new AxisAlignedBB(begin, end); + + return source.getWorld().getEntitiesInAABBexcluding(source.getEntity(), bb, e -> + !e.isDead && (e instanceof ICaster || e instanceof EntityPlayer) + ).stream().filter(e -> { + double dist = e.getDistance(origin.getX(), origin.getY(), origin.getZ()); + double dist2 = e.getDistance(origin.getX(), origin.getY() - e.getEyeHeight(), origin.getZ()); + + return dist <= radius || dist2 <= radius; + }) + .map(CasterUtils::toCaster) + .filter(o -> o.isPresent() && o.get() != source) + .map(Optional::get); + } + + static Stream> findAllSpellsInRange(ICaster source, AxisAlignedBB bb) { + return source.getWorld().getEntitiesInAABBexcluding(source.getEntity(), bb, e -> + !e.isDead && (e instanceof ICaster || Predicates.MAGI.test(e)) + ).stream() + .map(CasterUtils::toCaster) + .filter(o -> o.isPresent() && o.get() != source) + .map(Optional::get); + } + + @SuppressWarnings("unchecked") + static Optional toMagicEffect(Class type, @Nullable Entity entity) { + return toCaster(entity) + .filter(ICaster::hasEffect) + .map(caster -> caster.getEffect(type, false)) + .filter(e -> !e.getDead()); + } + + static Optional> toCaster(@Nullable Entity entity) { + + if (entity instanceof ICaster) { + return Optional.of((ICaster)entity); + } + + if (entity instanceof EntityPlayer) { + return Optional.of(PlayerSpeciesList.instance().getPlayer((EntityPlayer)entity)); + } + + return Optional.empty(); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/spell/ICaster.java b/src/main/java/com/minelittlepony/unicopia/spell/ICaster.java index b6651ba9..2977e374 100644 --- a/src/main/java/com/minelittlepony/unicopia/spell/ICaster.java +++ b/src/main/java/com/minelittlepony/unicopia/spell/ICaster.java @@ -14,6 +14,7 @@ import com.minelittlepony.util.vector.VecHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; +import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; @@ -23,7 +24,12 @@ public interface ICaster extends IOwned, ILevelle void setEffect(@Nullable IMagicEffect effect); @Nullable - IMagicEffect getEffect(boolean update); + default IMagicEffect getEffect(boolean update) { + return getEffect(null, update); + } + + @Nullable + T getEffect(@Nullable Class type, boolean update); @Nullable default IMagicEffect getEffect() { @@ -39,14 +45,23 @@ public interface ICaster extends IOwned, ILevelle return getOwner(); } + /** + * Gets the unique id associated with this caste. + */ default UUID getUniqueId() { return getEntity().getUniqueID(); } + /** + * gets the minecraft world + */ default World getWorld() { return getEntity().getEntityWorld(); } + /** + * Gets the center position where this caster is located. + */ default BlockPos getOrigin() { return getEntity().getPosition(); } @@ -69,6 +84,14 @@ public interface ICaster extends IOwned, ILevelle } } + default Stream> findAllSpellsInRange(double radius) { + return CasterUtils.findAllSpellsInRange(this, radius); + } + + default Stream> findAllSpellsInRange(AxisAlignedBB bb) { + return CasterUtils.findAllSpellsInRange(this, bb); + } + default Stream findAllEntitiesInRange(double radius) { return VecHelper.findAllEntitiesInRange(getEntity(), getWorld(), getOrigin(), radius); } diff --git a/src/main/java/com/minelittlepony/util/vector/VecHelper.java b/src/main/java/com/minelittlepony/util/vector/VecHelper.java index 4c819c77..6feb28ba 100644 --- a/src/main/java/com/minelittlepony/util/vector/VecHelper.java +++ b/src/main/java/com/minelittlepony/util/vector/VecHelper.java @@ -32,7 +32,7 @@ public class VecHelper { Vec3d pos = e.getPositionEyes(partialTicks); Vec3d look = e.getLook(partialTicks).scale(distance); - return e.world.rayTraceBlocks(pos, pos.add(look), false, false, true); + return e.getEntityWorld().rayTraceBlocks(pos, pos.add(look), false, false, true); } /**