From 189b1d5f1f41326becc614e32d78fbb2f5228530 Mon Sep 17 00:00:00 2001 From: Maximusbarcz Date: Sat, 21 Jan 2023 14:59:09 +0100 Subject: Something something kinda works other than removing the entity, making explosion and having a cool animation because minecraft said so? --- .../mayaqq/ygasi/abilities/mercenary/Offence2.java | 22 +++--- .../java/dev/mayaqq/ygasi/events/ClickEvent.java | 59 -------------- .../mayaqq/ygasi/events/PlayerConnectEvent.java | 2 + .../java/dev/mayaqq/ygasi/events/TickEvent.java | 60 +++++++++++++++ .../java/dev/mayaqq/ygasi/mixin/ItemMixin.java | 90 ++++++++++++++++++++-- .../dev/mayaqq/ygasi/mixin/PlayerEntityMixin.java | 2 +- .../dev/mayaqq/ygasi/registry/EventRegistry.java | 4 +- 7 files changed, 160 insertions(+), 79 deletions(-) delete mode 100644 src/main/java/dev/mayaqq/ygasi/events/ClickEvent.java create mode 100644 src/main/java/dev/mayaqq/ygasi/events/TickEvent.java diff --git a/src/main/java/dev/mayaqq/ygasi/abilities/mercenary/Offence2.java b/src/main/java/dev/mayaqq/ygasi/abilities/mercenary/Offence2.java index 19ffd49..78ff513 100644 --- a/src/main/java/dev/mayaqq/ygasi/abilities/mercenary/Offence2.java +++ b/src/main/java/dev/mayaqq/ygasi/abilities/mercenary/Offence2.java @@ -1,30 +1,24 @@ package dev.mayaqq.ygasi.abilities.mercenary; import dev.mayaqq.ygasi.util.AdvUtils; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityData; -import net.minecraft.entity.EntityType; -import net.minecraft.entity.SpawnReason; +import net.minecraft.entity.EntityPose; import net.minecraft.entity.decoration.ArmorStandEntity; -import net.minecraft.entity.mob.MobEntity; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.server.command.SummonCommand; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.text.Text; import net.minecraft.util.Hand; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Vec3d; -import net.minecraft.world.World; +import net.minecraft.util.math.EulerAngle; import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; public class Offence2 { public static Map> playerSwords = new HashMap<>(); + public static Map> playerSwordsBlacklist = new HashMap<>(); + public static void give(ServerPlayerEntity player) { if (!AdvUtils.getAdvancementProgress(player, "ygasi", "mercenary/offence2")) { AdvUtils.grantAdvancementCriterion(player, new Identifier("ygasi", "mercenary/offence2"), "unlocked_offence2"); @@ -38,10 +32,14 @@ public class Offence2 { public static void summonStand(ServerPlayerEntity player, Integer sword) { BlockPos pos = player.getBlockPos(); ServerWorld world = player.getWorld(); - ArmorStandEntity armorStand = new ArmorStandEntity(world, pos.getX(), pos.getY(), pos.getZ()); + ArmorStandEntity armorStand = new ArmorStandEntity(world, pos.getX(), pos.getY() - 1, pos.getZ()); armorStand.setCustomName(Text.of(armorStand.getUuidAsString())); - armorStand.setCustomNameVisible(true); armorStand.setStackInHand(Hand.MAIN_HAND, player.getMainHandStack()); + armorStand.setInvisible(true); + armorStand.setNoGravity(true); + armorStand.setNoDrag(true); + armorStand.setInvulnerable(true); + armorStand.setRightArmRotation(new EulerAngle(0F, 270F, 270F)); if (playerSwords.get(player.getUuidAsString()) != null) { playerSwords.get(player.getUuidAsString()).add(armorStand.getUuidAsString()); } else { diff --git a/src/main/java/dev/mayaqq/ygasi/events/ClickEvent.java b/src/main/java/dev/mayaqq/ygasi/events/ClickEvent.java deleted file mode 100644 index c735fa4..0000000 --- a/src/main/java/dev/mayaqq/ygasi/events/ClickEvent.java +++ /dev/null @@ -1,59 +0,0 @@ -package dev.mayaqq.ygasi.events; - -import dev.mayaqq.ygasi.abilities.mercenary.Offence2; -import dev.mayaqq.ygasi.util.AdvUtils; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; -import net.minecraft.item.SwordItem; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvent; -import net.minecraft.util.Identifier; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import static dev.mayaqq.ygasi.Ygasi.LOGGER; -import static dev.mayaqq.ygasi.abilities.mercenary.Offence2.playerSwords; - -public class ClickEvent { - public static Map playerCharge = new HashMap<>(); - public static Map swords = new HashMap<>(); - private static Integer tick = 0; - public static void onTick() { - ServerTickEvents.END_SERVER_TICK.register(server -> { - tick++; - server.getPlayerManager().getPlayerList().forEach(player -> { - if (playerSwords.get(player.getUuidAsString()) != null && player.getWorld().getEntity(UUID.fromString(playerSwords.get(player.getUuidAsString()).get(0))) != null) { - playerSwords.get(player.getUuidAsString()).forEach(uuid -> { - player.getWorld().getEntity(UUID.fromString(uuid)).setPos(player.getX(), player.getY(), player.getZ()); - }); - } - if (tick == 20) { - if (player.isSneaking() && player.getMainHandStack().getItem() instanceof SwordItem && AdvUtils.getAdvancementProgress(player, "ygasi", "mercenary/offence2")) { - if (swords.getOrDefault(player.getUuidAsString(), 0) >= 3) { - return; - } - if (playerCharge.getOrDefault(player.getUuidAsString(), 0) == 5) { - playerCharge.remove(player.getUuidAsString()); - - swords.put(player.getUuidAsString(), swords.getOrDefault(player.getUuidAsString(), 0) + 1); - Offence2.summonStand((ServerPlayerEntity) player, swords.get(player.getUuidAsString())); - player.playSound(SoundEvent.of(new Identifier("entity.experience_orb.pickup")), SoundCategory.PLAYERS, 1.0F, 1.0F); - LOGGER.info("Sword charge: " + swords.get(player.getUuidAsString())); - } else { - playerCharge.put(player.getUuidAsString(), playerCharge.getOrDefault(player.getUuidAsString(), 0) + 1); - player.playSound(SoundEvent.of(new Identifier("block.note_block.harp")), SoundCategory.PLAYERS, 1.0F, playerCharge.get(player.getUuidAsString()).floatValue() / 3); - LOGGER.info("Player charge: " + playerCharge.get(player.getUuidAsString())); - } - } else { - playerCharge.remove(player.getUuidAsString()); - } - } - }); - if (tick == 20) { - tick = 0; - } - }); - } -} diff --git a/src/main/java/dev/mayaqq/ygasi/events/PlayerConnectEvent.java b/src/main/java/dev/mayaqq/ygasi/events/PlayerConnectEvent.java index 6723d01..19649cc 100644 --- a/src/main/java/dev/mayaqq/ygasi/events/PlayerConnectEvent.java +++ b/src/main/java/dev/mayaqq/ygasi/events/PlayerConnectEvent.java @@ -8,6 +8,7 @@ import net.minecraft.item.ItemStack; import java.util.UUID; import static dev.mayaqq.ygasi.abilities.mercenary.Offence2.playerSwords; +import static dev.mayaqq.ygasi.events.TickEvent.swords; import static dev.mayaqq.ygasi.registry.ItemRegistry.SKILL_BOOK; public class PlayerConnectEvent { @@ -28,6 +29,7 @@ public class PlayerConnectEvent { sword.remove(Entity.RemovalReason.DISCARDED); }); playerSwords.remove(handler.player.getUuidAsString()); + swords.remove(handler.player.getUuidAsString()); } }); } diff --git a/src/main/java/dev/mayaqq/ygasi/events/TickEvent.java b/src/main/java/dev/mayaqq/ygasi/events/TickEvent.java new file mode 100644 index 0000000..ae6fc76 --- /dev/null +++ b/src/main/java/dev/mayaqq/ygasi/events/TickEvent.java @@ -0,0 +1,60 @@ +package dev.mayaqq.ygasi.events; + +import dev.mayaqq.ygasi.abilities.mercenary.Offence2; +import dev.mayaqq.ygasi.util.AdvUtils; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.minecraft.item.SwordItem; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvent; +import net.minecraft.util.Identifier; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import static dev.mayaqq.ygasi.Ygasi.LOGGER; +import static dev.mayaqq.ygasi.abilities.mercenary.Offence2.playerSwords; +import static dev.mayaqq.ygasi.abilities.mercenary.Offence2.playerSwordsBlacklist; + +public class TickEvent { + public static Map playerCharge = new HashMap<>(); + public static Map swords = new HashMap<>(); + private static Integer tick = 0; + public static void onTick() { + ServerTickEvents.END_SERVER_TICK.register(server -> { + tick++; + server.getPlayerManager().getPlayerList().forEach(player -> { + if (playerSwords.get(player.getUuidAsString()) != null && playerSwordsBlacklist.get(player.getUuidAsString()) == null) { + playerSwords.get(player.getUuidAsString()).forEach(uuid -> { + player.getWorld().getEntity(UUID.fromString(uuid)).setPos(player.getX(), player.getY() + 2, player.getZ()); + }); + } + if (tick == 20) { + if (player.isSneaking() && player.getMainHandStack().getItem() instanceof SwordItem && AdvUtils.getAdvancementProgress(player, "ygasi", "mercenary/offence2")) { + if (swords.getOrDefault(player.getUuidAsString(), 0) >= 3) { + return; + } + if (playerCharge.getOrDefault(player.getUuidAsString(), 0) == 5) { + playerCharge.remove(player.getUuidAsString()); + + swords.put(player.getUuidAsString(), swords.getOrDefault(player.getUuidAsString(), 0) + 1); + Offence2.summonStand(player, swords.get(player.getUuidAsString())); + player.playSound(SoundEvent.of(new Identifier("entity.experience_orb.pickup")), SoundCategory.PLAYERS, 1.0F, 1.0F); + LOGGER.info("Sword charge: " + swords.get(player.getUuidAsString())); + } else { + playerCharge.put(player.getUuidAsString(), playerCharge.getOrDefault(player.getUuidAsString(), 0) + 1); + player.playSound(SoundEvent.of(new Identifier("block.note_block.harp")), SoundCategory.PLAYERS, 1.0F, playerCharge.get(player.getUuidAsString()).floatValue() / 3); + LOGGER.info("Player charge: " + playerCharge.get(player.getUuidAsString())); + } + } else { + playerCharge.remove(player.getUuidAsString()); + } + } + }); + if (tick == 20) { + tick = 0; + } + }); + } +} diff --git a/src/main/java/dev/mayaqq/ygasi/mixin/ItemMixin.java b/src/main/java/dev/mayaqq/ygasi/mixin/ItemMixin.java index f89d7c6..8cfbe8b 100644 --- a/src/main/java/dev/mayaqq/ygasi/mixin/ItemMixin.java +++ b/src/main/java/dev/mayaqq/ygasi/mixin/ItemMixin.java @@ -1,27 +1,107 @@ package dev.mayaqq.ygasi.mixin; -import dev.mayaqq.ygasi.events.ClickEvent; +import dev.mayaqq.ygasi.events.TickEvent; +import dev.mayaqq.ygasi.util.Multithreading; +import net.minecraft.block.Block; +import net.minecraft.block.Blocks; +import net.minecraft.entity.Entity; +import net.minecraft.entity.damage.DamageSource; +import net.minecraft.entity.decoration.ArmorStandEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.SwordItem; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.EulerAngle; +import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; +import org.slf4j.Logger; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import static dev.mayaqq.ygasi.Ygasi.LOGGER; +import java.util.ArrayList; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +import static dev.mayaqq.ygasi.abilities.mercenary.Offence2.playerSwords; +import static dev.mayaqq.ygasi.abilities.mercenary.Offence2.playerSwordsBlacklist; @Mixin(Item.class) public class ItemMixin { + @Shadow @Final private static Logger LOGGER; + @Inject(method = "use", at = @At("HEAD")) private void injectRightClickBehavior(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable> cir) { - if (user.getMainHandStack().getItem() instanceof SwordItem && ClickEvent.swords.getOrDefault(user.getUuidAsString(), 0) >= 1) { - LOGGER.info("Player " + user.getEntityName() + " launched " + ClickEvent.swords.get(user.getUuidAsString()) + " swords!"); - ClickEvent.swords.remove(user.getUuidAsString()); + if (user.getMainHandStack().getItem() instanceof SwordItem && TickEvent.swords.getOrDefault(user.getUuidAsString(), 0) >= 1) { + LOGGER.info("Player " + user.getEntityName() + " launched " + TickEvent.swords.get(user.getUuidAsString()) + " swords!"); + ArrayList swords = new ArrayList<>(); + playerSwords.get(user.getUuidAsString()).forEach(uuid -> { + ServerPlayerEntity player = (ServerPlayerEntity) user; + ArmorStandEntity sword = (ArmorStandEntity) player.getWorld().getEntity(UUID.fromString(uuid)); + swords.add(uuid); + float yaw = player.getYaw(1); + float pitch = player.getPitch(1); + yaw = (float) (yaw - Math.PI); + pitch = (float) (pitch + Math.PI / 2); + sword.setRightArmRotation(new EulerAngle(pitch, yaw, sword.getRightArmRotation().getRoll())); + Multithreading.schedule(() -> { + playerSwordsBlacklist.put(player.getUuidAsString(), swords); + ServerWorld serverWorld = (ServerWorld) world; + boolean isAir = true; + while (isAir) { + Vec3d rotation = player.getRotationVec(1.0f); + sword.setPos(sword.getX() + rotation.x, sword.getY() + rotation.y, sword.getZ() + rotation.z); + BlockPos[] relativeBlockPositions = {new BlockPos(1, 1.5F, 0), new BlockPos(-1, 1.5F, 0), new BlockPos(0, 1.5F, 1), new BlockPos(0, 1.5F, -1)}; + BlockPos pos = sword.getBlockPos(); + boolean hitTarget = true; + for (BlockPos relativeBlockPos : relativeBlockPositions) { + BlockPos blockPos = pos.add(relativeBlockPos); + Block block = sword.world.getBlockState(blockPos).getBlock(); + if(block != Blocks.AIR) { + LOGGER.info("Sword hit a block!"); + hitTarget = onHit(serverWorld, player, sword, hitTarget); + isAir = false; + } + double entityCheckRadius = 1; + for (PlayerEntity pl : world.getPlayers()) { + if (pl.distanceTo(sword) <= entityCheckRadius && pl != player && pl.getScoreboardTeam() != player.getScoreboardTeam()) { + LOGGER.info("Sword hit a player!"); + //create an explosion at the position of the sword + hitTarget = onHit(serverWorld, player, sword, hitTarget); + isAir = false; + } + } + } + try { + Thread.sleep(30); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + }, 1, TimeUnit.SECONDS); + }); + Multithreading.schedule(() -> { + playerSwords.remove(user.getUuidAsString()); + TickEvent.swords.remove(user.getUuidAsString()); + playerSwordsBlacklist.remove(user.getUuidAsString()); + }, 2, TimeUnit.SECONDS); + } + } + public boolean onHit(ServerWorld world, ServerPlayerEntity player, ArmorStandEntity sword, Boolean hitTraget) { + if (hitTraget) { + world.createExplosion(player, DamageSource.player(player), null, sword.getX(), sword.getY(), sword.getZ(), 10, false, World.ExplosionSourceType.MOB, false); + sword.remove(Entity.RemovalReason.DISCARDED); + return false; } + return true; } } diff --git a/src/main/java/dev/mayaqq/ygasi/mixin/PlayerEntityMixin.java b/src/main/java/dev/mayaqq/ygasi/mixin/PlayerEntityMixin.java index a9687b1..f0efc1d 100644 --- a/src/main/java/dev/mayaqq/ygasi/mixin/PlayerEntityMixin.java +++ b/src/main/java/dev/mayaqq/ygasi/mixin/PlayerEntityMixin.java @@ -22,7 +22,7 @@ public abstract class PlayerEntityMixin { public void onAttack(Entity target, CallbackInfo ci) { if (this.isPlayer()) { ServerPlayerEntity player = (ServerPlayerEntity) (Object) this; - if (!attackList.containsKey(player.getUuidAsString() + target.getUuidAsString()) && AdvUtils.getAdvancementProgress(player, "ygasi", "mercenary/offence1")) { + if (!attackList.containsKey(player.getUuidAsString() + target.getUuidAsString()) && AdvUtils.getAdvancementProgress(player, "ygasi", "mercenary/offence1") && !target.isInvulnerable()) { attackList.put(player.getUuidAsString() + target.getUuidAsString(), true); StatusEffectInstance currentEffect = player.getStatusEffect(StatusEffects.STRENGTH); int currentLevel = currentEffect != null ? currentEffect.getAmplifier() : 0; diff --git a/src/main/java/dev/mayaqq/ygasi/registry/EventRegistry.java b/src/main/java/dev/mayaqq/ygasi/registry/EventRegistry.java index 66b6be2..2edb22e 100644 --- a/src/main/java/dev/mayaqq/ygasi/registry/EventRegistry.java +++ b/src/main/java/dev/mayaqq/ygasi/registry/EventRegistry.java @@ -1,12 +1,12 @@ package dev.mayaqq.ygasi.registry; -import dev.mayaqq.ygasi.events.ClickEvent; +import dev.mayaqq.ygasi.events.TickEvent; import dev.mayaqq.ygasi.events.PlayerConnectEvent; public class EventRegistry { public static void register() { PlayerConnectEvent.onPlayerConnect(); PlayerConnectEvent.onPlayerDisconnect(); - ClickEvent.onTick(); + TickEvent.onTick(); } } -- cgit