Skip to content

Commit

Permalink
wip active minions & animation
Browse files Browse the repository at this point in the history
  • Loading branch information
MatzHilven committed Jun 25, 2024
1 parent 7fd3437 commit e045786
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,11 @@
import net.minestom.server.item.component.HeadProfile;
import org.jetbrains.annotations.NotNull;

public class SkinnableHead {
private final String texture;

/**
* @param texture The base64 texture of player head
*/
public SkinnableHead(@NotNull String texture) {
this.texture = texture;
}

public @NotNull ItemStack.Builder builder() {
public class ItemStackUtils {
public static @NotNull ItemStack.Builder getTexturedHead(String texture) {
ItemStack.Builder skull = ItemStack.builder(Material.PLAYER_HEAD);
HeadProfile profile = new HeadProfile(new PlayerSkin(this.texture, null));
HeadProfile profile = new HeadProfile(new PlayerSkin(texture, null));
skull.set(ItemComponent.PROFILE, profile);
return skull;
}

public @NotNull ItemStack build() {
return builder().build();
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/slampvp/factory/minion/MinionListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ public MinionListener() {
return;
}

Optional<Minion> minion = Minion.byId(minionId);
if (minion.isEmpty()) {
Optional<Minion> optionalMinion = Minion.byId(minionId);
if (optionalMinion.isEmpty()) {
return;
}

event.setCancelled(true);
player.setItemInMainHand(itemInHand.withAmount(itemInHand.amount() - 1));
minionManager.addMinion(player, event.getBlockPosition(), optionalMinion.get());
});
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/slampvp/factory/minion/MinionManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.slampvp.factory.minion;

import com.slampvp.factory.FactoryServer;
import com.slampvp.factory.minion.models.Minion;
import net.minestom.server.coordinate.BlockVec;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.metadata.display.ItemDisplayMeta;

public final class MinionManager {
private static MinionManager instance;
Expand All @@ -23,4 +31,20 @@ public void init() {
FactoryServer.LOGGER.info("Initializing Minion Manager ✔");
}

public void addMinion(Player player, BlockVec position, Minion minion) {
Entity displayEntity = new Entity(EntityType.ITEM_DISPLAY);
displayEntity.setNoGravity(true);

ItemDisplayMeta meta = (ItemDisplayMeta) displayEntity.getEntityMeta();

meta.setItemStack(minion.getItem());
meta.setScale(new Vec(1.5f, 1.5f, 1.5f));

meta.setLeftRotation(new float[]{0.0f, 0.0f, 0.0f, 1.0f});
meta.setRightRotation(new float[]{0.0f, 1.0f, 0.0f, 0.0f});
meta.setTranslation(new Vec(2.0f, 0.0f, -1.0f));

displayEntity.setInstance(player.getInstance(), position.add(0.5, 3, 0.5));
displayEntity.teleport(new Pos(position).add(0.5, 3, 0.5));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.slampvp.factory.minion.models;

import net.minestom.server.coordinate.BlockVec;

public record ActiveMinion(Minion minion, BlockVec position, BlockVec chestPosition) {

}
8 changes: 4 additions & 4 deletions src/main/java/com/slampvp/factory/minion/models/Minion.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ public sealed interface Minion extends Minions permits MinionImpl {
}

@NotNull
String getId();
String id();

@NotNull
TextComponent getName();
TextComponent name();

@NotNull
TextComponent getLore();
TextComponent lore();

@NotNull
String getTexture();
String texture();

@NotNull
ItemStack getItem();
Expand Down
53 changes: 8 additions & 45 deletions src/main/java/com/slampvp/factory/minion/models/MinionImpl.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package com.slampvp.factory.minion.models;

import com.slampvp.factory.common.SkinnableHead;
import com.slampvp.factory.common.ItemStackUtils;
import net.kyori.adventure.text.TextComponent;
import net.minestom.server.item.ItemStack;
import net.minestom.server.tag.Tag;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.*;

final class MinionImpl implements Minion {
record MinionImpl(String id, TextComponent name, TextComponent lore, String texture) implements Minion {
private static final Set<Minion> MINIONS = new HashSet<>();

private final String id;
private final TextComponent name;
private final TextComponent lore;
private final String texture;

public MinionImpl(@NotNull String id, @NotNull TextComponent name, @NotNull TextComponent lore, @NotNull String texture) {
MinionImpl(@NotNull String id, @NotNull TextComponent name, @NotNull TextComponent lore, @NotNull String texture) {
this.id = id;
this.name = name;
this.lore = lore;
Expand All @@ -30,49 +22,20 @@ public MinionImpl(@NotNull String id, @NotNull TextComponent name, @NotNull Text

static @NotNull Optional<Minion> getById(String id) {
System.out.println(MINIONS);
return MINIONS.stream().filter(minion -> minion.getId().equals(id)).findFirst();
return MINIONS.stream().filter(minion -> minion.id().equals(id)).findFirst();
}

static @NotNull Set<Minion> getAll() {
return Collections.unmodifiableSet(MINIONS);
}

@Override
public @NotNull String getId() {
return this.id;
}

@Override
public @NotNull TextComponent getName() {
return this.name;
}

@Override
public @NotNull TextComponent getLore() {
return this.lore;
}

@Override
public @NotNull String getTexture() {
return this.texture;
}

@Override
public @NotNull ItemStack getItem() {
return new SkinnableHead(this.texture)
.builder()
return ItemStackUtils
.getTexturedHead(this.texture)
.customName(this.name)
.lore(this.lore)
.build()
.withTag(Tag.String("minion"), getId());
}

@Override
public String toString() {
return "MinionImpl{" +
"id='" + id + '\'' +
", name=" + name.content() +
", lore=" + lore.content() +
'}';
.withTag(Tag.String("minion"), id());
}
}

0 comments on commit e045786

Please sign in to comment.