Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void shopSetup() {
- // Create a default Steve skin
- Skin shopSkin = new Skin();
- shopSkin.setSkinId("shopkeeper");
- shopSkin.setSkinData(new byte[8192]); // default blank skin data
- shopSkin.setCapeData(new byte[0]);
- shopSkin.setGeometryName("geometry.humanoid.custom");
- shopSkin.setGeometryData(Arrays.toString(new byte[0]));
- // Spawn a test NPC at your spawn
- Vector3 testPos = new Vector3(65.5, 6, -45); // slightly above ground
- CallbackNPC testNPC = new CallbackNPC(level, testPos, "TestShop", shopSkin, p -> {});
- testNPC.spawnToAll();
- for (Team t : teams.values()) {
- NPCShop npcShop = new NPCShop(t.getColorCode() + t.getColor() + " Team Shop");
- npcShop.addItem(Item.get(Item.STONE_SWORD, 0, 1), 10, NPCShop.Currency.IRON);
- npcShop.addItem(Item.get(Item.IRON_INGOT, 0, 16), 5, NPCShop.Currency.GOLD);
- Vector3 shopPos = t.getSpawn().add(3, 0, 0);
- CallbackNPC shopNPC = new CallbackNPC(level, shopPos, t.getColorCode() + t.getColor() + " Team Shop", shopSkin, player -> {
- SimpleForm form = new SimpleForm(npcShop.getName(), "Select an item to buy:");
- npcShop.getItems().forEach((itemKey, shopItem) -> form.addButton(shopItem.item().getName() +
- " §7- Price: " + shopItem.price() + " " + shopItem.currency().name()));
- form.send(player);
- });
- npcs.add(shopNPC); // store strong reference
- level.getChunk(shopPos.getFloorX() >> 4, shopPos.getFloorZ() >> 4, true); // force chunk load
- shopNPC.spawnToAll();
- }
- }
- public abstract class AbstractNPC extends EntityHuman {
- protected final String npcName;
- public AbstractNPC(@NotNull Level level, @NotNull Vector3 pos, @NotNull String name, @NotNull Skin skin) {
- super(level.getChunk((int) pos.x >> 4, (int) pos.z >> 4), new CompoundTag()
- .putList("Pos", new ListTag<DoubleTag>()
- .add(new DoubleTag(pos.x))
- .add(new DoubleTag(pos.y))
- .add(new DoubleTag(pos.z)))
- .putList("Motion", new ListTag<DoubleTag>()
- .add(new DoubleTag(0.0))
- .add(new DoubleTag(0.0))
- .add(new DoubleTag(0.0)))
- .putList("Rotation", new ListTag<FloatTag>()
- .add(new FloatTag(0f))
- .add(new FloatTag(0f)))
- .putCompound("Skin", new CompoundTag()) // empty skin
- .putString("NameTag", name)
- );
- this.setSkin(skin); // must be called BEFORE spawn
- this.npcName = name;
- this.setNameTag(name);
- this.setNameTagVisible(true);
- this.setNameTagAlwaysVisible(true);
- this.setPosition(pos);
- }
- /**
- * Called when a player interacts with this NPC
- * Implement in subclasses to open shop/upgrade menus
- */
- public abstract void handleInteraction(@NotNull Player player);
- /**
- * Spawns this NPC to all players in the level
- */
- public void spawnToAll() {
- level.loadChunk(getPosition().getFloorX() >> 4, getPosition().getFloorZ() >> 4);
- for (Player p : level.getPlayers().values()) {
- this.spawnTo(p);
- }
- }
- }
- public class CallbackNPC extends AbstractNPC {
- private final Consumer<Player> onInteract;
- public CallbackNPC(Level level, Vector3 pos, String name, Skin skin, Consumer<Player> onInteract) {
- super(level, pos, name, skin);
- this.onInteract = onInteract;
- }
- @Override
- public void handleInteraction(@NotNull Player player) {
- onInteract.accept(player);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment