aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/mayaqq/ygasi/mixin/ItemMixin.java
diff options
context:
space:
mode:
authorMaximusbarcz <maxim.baranek@gmail.com>2023-01-21 14:59:09 +0100
committerMaximusbarcz <maxim.baranek@gmail.com>2023-01-21 14:59:09 +0100
commit189b1d5f1f41326becc614e32d78fbb2f5228530 (patch)
tree7f0f8295b70a7757bb4d33363236a34c1f59fdef /src/main/java/dev/mayaqq/ygasi/mixin/ItemMixin.java
parentadaecef7df47ccdbfbda6f438c28696ff1ce8822 (diff)
downloadygasi-189b1d5f1f41326becc614e32d78fbb2f5228530.tar.gz
ygasi-189b1d5f1f41326becc614e32d78fbb2f5228530.tar.bz2
ygasi-189b1d5f1f41326becc614e32d78fbb2f5228530.zip
Something something kinda works other than removing the entity, making explosion and having a cool animation because minecraft said so?
Diffstat (limited to 'src/main/java/dev/mayaqq/ygasi/mixin/ItemMixin.java')
-rw-r--r--src/main/java/dev/mayaqq/ygasi/mixin/ItemMixin.java90
1 files changed, 85 insertions, 5 deletions
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<TypedActionResult<ItemStack>> 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<String> 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;
}
}