Hub / src / main / java / com / lifeknight / relaymchub / cosmetics / BlockMorphCosmetic.java
BlockMorphCosmetic.java
Raw
package com.lifeknight.relaymchub.cosmetics;

import com.lifeknight.relaymchub.Main;
import com.lifeknight.relaymchub.player.HubPlayer;
import com.lifeknight.relayutils.basic.Miscellaneous;
import com.lifeknight.relayutils.player.cosmetics.Cosmetic;
import com.lifeknight.relayutils.utilities.PlayerUtilities;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.entity.item.EntityFallingBlock;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftFallingBlock;
import org.bukkit.entity.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BlockMorphCosmetic extends MorphCosmetic {
    private final Material material;
    private final Map<ArmorStand, List<Entity>> fallingBlockComponents = new HashMap<>();

    public BlockMorphCosmetic(Cosmetic original, Material material) {
        super(original, EntityType.FALLING_BLOCK);

        this.material = material;
    }

    @Override
    public Entity getNewEntity(Location location, HubPlayer hubPlayer) {
        ArmorStand armorStand = Main.getWorld().spawn(location.add(0, -1, 0), ArmorStand.class);
        armorStand.setGravity(false);
        armorStand.setCustomNameVisible(false);
        armorStand.setVisible(false);
        armorStand.setMarker(true);

        FallingBlock fallingBlock = Main.getWorld().spawnFallingBlock(location.add(0, 0.5, 0), this.material.createBlockData());
        EntityFallingBlock entityFallingBlock = ((CraftFallingBlock) fallingBlock).getHandle();
        NBTTagCompound compound = entityFallingBlock.d;
        if (compound == null) {
            compound = new NBTTagCompound();
        }
        compound.a("Time", 1);
        entityFallingBlock.f(compound);
        fallingBlock.setGravity(false);
        //fallingBlock.setCustomName(hubPlayer.getFormattedName());
        fallingBlock.setCustomNameVisible(false);

        armorStand.addPassenger(fallingBlock);

/*
        Shulker shulker = Main.getWorld().spawn(location.add(0, 0.5, 0), Shulker.class);

        shulker.setAI(false);
        shulker.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 2000000, 0).withParticles(false));

        armorStand.addPassenger(shulker);
*/

        this.fallingBlockComponents.put(armorStand, Miscellaneous.getList(fallingBlock/*, shulker*/));

        return armorStand;
    }

    @Override
    public void doDisable(HubPlayer hubPlayer) {
        ArmorStand armorStand = (ArmorStand) this.playerToMorph.get(hubPlayer);

        List<Entity> fallingBlock = this.fallingBlockComponents.get(armorStand);
        if (fallingBlock != null) {
            for (Entity entity : fallingBlock) {
                entity.remove();
            }
        }
        super.doDisable(hubPlayer);
    }

    @Override
    public void tick(HubPlayer hubPlayer) {
        Entity entity = this.playerToMorph.get(hubPlayer);

        if (entity != null) {
            Player player = hubPlayer.getPlayer();
            entity.setVelocity(player.getVelocity());
            List<Entity> others = this.fallingBlockComponents.get(entity);
            Entity fallingBlock = others.get(0);
            if (fallingBlock.isDead()) {
                others.clear();
                fallingBlock = Main.getWorld().spawnFallingBlock(player.getLocation().add(0, 0.5, 0), this.material.createBlockData());
                EntityFallingBlock entityFallingBlock = ((CraftFallingBlock) fallingBlock).getHandle();
                NBTTagCompound compound = entityFallingBlock.d;
                if (compound == null) {
                    compound = new NBTTagCompound();
                }
                compound.a("Time", 1);
                entityFallingBlock.f(compound);
                fallingBlock.setGravity(false);
                fallingBlock.setCustomNameVisible(true);
                fallingBlock.setCustomName(hubPlayer.getFormattedName());
                others.add(fallingBlock);
            }
            entity.removePassenger(others.get(0));
            entity.teleport(player.getLocation());
            entity.addPassenger(others.get(0));
            if (entity instanceof EnderDragon) {
                PlayerUtilities.setEntityDirection(entity, player);
            }
            entity.setFireTicks(0);
        }

        if (!hubPlayer.getPlayer().isInvisible()) {
            this.disable(hubPlayer, true);
        }
    }

    @Override
    public BlockMorphCosmetic setProjectile(Material material) {
        return (BlockMorphCosmetic) super.setProjectile(material);
    }

    @Override
    public BlockMorphCosmetic setProjectile(EntityType entityType) {
        return (BlockMorphCosmetic) super.setProjectile(entityType);
    }
}