From 9e6da2f64a1f4d54ba58ab878efa1b84e65cb444 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 3 May 2020 22:42:16 +0200 Subject: [PATCH] Fixed projectiles and other entities not spawning, fixed the staff --- .../unicopia/entity/ProjectileEntity.java | 10 +++- .../unicopia/item/EnchantedStaffItem.java | 15 +++--- .../unicopia/magic/Castable.java | 3 +- .../unicopia/magic/TossedMagicEffect.java | 3 +- .../unicopia/network/Channel.java | 1 + .../unicopia/network/MsgSpawnProjectile.java | 50 +++++++++++++++++++ 6 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java diff --git a/src/main/java/com/minelittlepony/unicopia/entity/ProjectileEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/ProjectileEntity.java index a7ad62b4..1f8bdced 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/ProjectileEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/ProjectileEntity.java @@ -7,7 +7,9 @@ import com.minelittlepony.unicopia.magic.Caster; import com.minelittlepony.unicopia.magic.MagicEffect; import com.minelittlepony.unicopia.magic.TossedMagicEffect; import com.minelittlepony.unicopia.magic.spell.SpellRegistry; +import com.minelittlepony.unicopia.network.Channel; import com.minelittlepony.unicopia.network.EffectSync; +import com.minelittlepony.unicopia.network.MsgSpawnProjectile; import com.minelittlepony.unicopia.util.projectile.AdvancedProjectile; import com.minelittlepony.unicopia.util.projectile.Tossable; import com.minelittlepony.unicopia.util.projectile.TossableItem; @@ -24,6 +26,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.Packet; import net.minecraft.particle.ItemStackParticleEffect; import net.minecraft.particle.ParticleEffect; import net.minecraft.particle.ParticleTypes; @@ -205,7 +208,7 @@ public class ProjectileEntity extends ThrownItemEntity implements IMagicals, Adv @Override public void launch(Entity entityThrower, float pitch, float yaw, float wobble, float velocity, float inaccuracy) { - setVelocity(pitch, yaw, wobble, velocity, inaccuracy); + setProperties(entityThrower, pitch, yaw, wobble, velocity, inaccuracy); } private ParticleEffect getParticleParameters() { @@ -289,4 +292,9 @@ public class ProjectileEntity extends ThrownItemEntity implements IMagicals, Adv remove(); } } + + @Override + public Packet createSpawnPacket() { + return Channel.SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this)); + } } diff --git a/src/main/java/com/minelittlepony/unicopia/item/EnchantedStaffItem.java b/src/main/java/com/minelittlepony/unicopia/item/EnchantedStaffItem.java index d23b912d..fb30289d 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/EnchantedStaffItem.java +++ b/src/main/java/com/minelittlepony/unicopia/item/EnchantedStaffItem.java @@ -16,7 +16,6 @@ import com.minelittlepony.unicopia.util.projectile.TossableItem; import net.minecraft.block.BlockState; import net.minecraft.client.item.TooltipContext; -import net.minecraft.entity.Entity; import net.minecraft.entity.EquipmentSlot; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.damage.DamageSource; @@ -26,7 +25,6 @@ import net.minecraft.particle.ParticleTypes; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; -import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; import net.minecraft.util.math.BlockPos; @@ -54,9 +52,8 @@ public class EnchantedStaffItem extends StaffItem implements Affine, TossableIte if (EquinePredicates.PLAYER_UNICORN.test(player) && hand == Hand.MAIN_HAND) { ItemStack itemstack = player.getStackInHand(hand); - player.swingHand(hand); - - return new TypedActionResult<>(ActionResult.SUCCESS, itemstack); + player.setCurrentHand(hand); + return TypedActionResult.consume(itemstack); } return super.use(world, player, hand); @@ -91,15 +88,15 @@ public class EnchantedStaffItem extends StaffItem implements Affine, TossableIte return false; } - public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) { - + @Override + public void usageTick(World world, LivingEntity entity, ItemStack stack, int ticksRemaining) { if (entity instanceof LivingEntity) { - LivingEntity living = (LivingEntity)entity; + LivingEntity living = entity; if (living.getActiveItem().getItem() == this) { Vec3d eyes = entity.getCameraPosVec(1); - float i = getMaxUseTime(stack) - living.getItemUseTimeLeft(); + float i = getMaxUseTime(stack) - ticksRemaining; world.addParticle(i > 150 ? ParticleTypes.LARGE_SMOKE : ParticleTypes.CLOUD, eyes.x, eyes.y, eyes.z, (world.random.nextGaussian() - 0.5) / 10, diff --git a/src/main/java/com/minelittlepony/unicopia/magic/Castable.java b/src/main/java/com/minelittlepony/unicopia/magic/Castable.java index 7ad4eeb8..f1291c0a 100644 --- a/src/main/java/com/minelittlepony/unicopia/magic/Castable.java +++ b/src/main/java/com/minelittlepony/unicopia/magic/Castable.java @@ -1,6 +1,7 @@ package com.minelittlepony.unicopia.magic; import com.minelittlepony.unicopia.entity.SpellcastEntity; +import com.minelittlepony.unicopia.entity.UEntities; import com.minelittlepony.unicopia.magic.spell.SpellRegistry; import net.minecraft.item.ItemStack; @@ -46,7 +47,7 @@ public interface Castable extends MagicalItem, Dispensable { * Called to cast a spell. The result is an entity spawned with the spell attached. */ default SpellcastEntity castContainedSpell(World world, BlockPos pos, ItemStack stack, MagicEffect effect) { - SpellcastEntity spell = new SpellcastEntity(null, world); + SpellcastEntity spell = new SpellcastEntity(UEntities.MAGIC_SPELL, world); spell.setAffinity(getAffinity(stack)); spell.updatePositionAndAngles(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, 0, 0); diff --git a/src/main/java/com/minelittlepony/unicopia/magic/TossedMagicEffect.java b/src/main/java/com/minelittlepony/unicopia/magic/TossedMagicEffect.java index 0423a502..21cbabcf 100644 --- a/src/main/java/com/minelittlepony/unicopia/magic/TossedMagicEffect.java +++ b/src/main/java/com/minelittlepony/unicopia/magic/TossedMagicEffect.java @@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.magic; import javax.annotation.Nullable; import com.minelittlepony.unicopia.entity.ProjectileEntity; +import com.minelittlepony.unicopia.entity.UEntities; import com.minelittlepony.unicopia.item.UItems; import com.minelittlepony.unicopia.magic.spell.SpellRegistry; import com.minelittlepony.unicopia.util.projectile.AdvancedProjectile; @@ -49,7 +50,7 @@ public interface TossedMagicEffect extends MagicEffect, Tossable> { world.playSound(null, entity.getX(), entity.getY(), entity.getZ(), getThrowSound(caster), SoundCategory.NEUTRAL, 0.7F, 0.4F / (world.random.nextFloat() * 0.4F + 0.8F)); if (caster.isLocal()) { - AdvancedProjectile projectile = new ProjectileEntity(null, world, caster.getOwner()); + AdvancedProjectile projectile = new ProjectileEntity(UEntities.THROWN_ITEM, world, caster.getOwner()); projectile.setItem(getCastAppearance(caster)); projectile.setThrowDamage(getThrowDamage(caster)); diff --git a/src/main/java/com/minelittlepony/unicopia/network/Channel.java b/src/main/java/com/minelittlepony/unicopia/network/Channel.java index 8872d4c7..869f9203 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/Channel.java +++ b/src/main/java/com/minelittlepony/unicopia/network/Channel.java @@ -20,6 +20,7 @@ public interface Channel { SPacketType BROADCAST_CAPABILITIES = broadcast(PLAYER_CAPABILITIES, MsgPlayerCapabilities::new); CPacketType SPAWN_RAINBOW = serverToClient(new Identifier("unicopia", "rainbow_entity"), MsgSpawnRainbow::new); + CPacketType SPAWN_PROJECTILE = serverToClient(new Identifier("unicopia", "projectile_entity"), MsgSpawnProjectile::new); static void bootstrap() { } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java b/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java new file mode 100644 index 00000000..eecaf549 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java @@ -0,0 +1,50 @@ +package com.minelittlepony.unicopia.network; + +import java.io.IOException; + +import net.fabricmc.fabric.api.network.PacketContext; +import net.minecraft.client.network.packet.EntitySpawnS2CPacket; +import net.minecraft.client.world.ClientWorld; +import net.minecraft.entity.Entity; +import net.minecraft.util.PacketByteBuf; +import net.minecraft.world.World; + +public class MsgSpawnProjectile extends EntitySpawnS2CPacket implements Channel.Packet { + + MsgSpawnProjectile(PacketByteBuf buffer) { + try { + read(buffer); + } catch (IOException e) { } + } + + public MsgSpawnProjectile(Entity e) { + super(e); + } + + @Override + public void toBuffer(PacketByteBuf buffer) { + try { + write(buffer); + } catch (IOException e) { + } + } + + @Override + public void handle(PacketContext context) { + World world = context.getPlayer().world; + Entity entity = getEntityTypeId().create(world); + + entity.updateTrackedPosition(getX(), getY(), getZ()); + entity.pitch = getPitch() * 360 / 256.0F; + entity.yaw = getYaw() * 360 / 256.0F; + entity.setEntityId(getId()); + entity.setUuid(getUuid()); + ((ClientWorld)world).addEntity(getId(), entity); + } +} + + + + + +