SilentWolf_6662

Shop NPC

Aug 27th, 2025
55
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | Source Code | 0 0
  1.     private void shopSetup() {
  2.         // Create a default Steve skin
  3.         Skin shopSkin = new Skin();
  4.         shopSkin.setSkinId("shopkeeper");
  5.         shopSkin.setSkinData(new byte[8192]); // default blank skin data
  6.         shopSkin.setCapeData(new byte[0]);
  7.         shopSkin.setGeometryName("geometry.humanoid.custom");
  8.         shopSkin.setGeometryData(Arrays.toString(new byte[0]));
  9.  
  10.         // Spawn a test NPC at your spawn
  11.         Vector3 testPos = new Vector3(65.5, 6, -45); // slightly above ground
  12.         CallbackNPC testNPC = new CallbackNPC(level, testPos, "TestShop", shopSkin, p -> {});
  13.         testNPC.spawnToAll();
  14.  
  15.         for (Team t : teams.values()) {
  16.             NPCShop npcShop = new NPCShop(t.getColorCode() + t.getColor() + " Team Shop");
  17.             npcShop.addItem(Item.get(Item.STONE_SWORD, 0, 1), 10, NPCShop.Currency.IRON);
  18.             npcShop.addItem(Item.get(Item.IRON_INGOT, 0, 16), 5, NPCShop.Currency.GOLD);
  19.  
  20.             Vector3 shopPos = t.getSpawn().add(3, 0, 0);
  21.  
  22.             CallbackNPC shopNPC = new CallbackNPC(level, shopPos, t.getColorCode() + t.getColor() + " Team Shop", shopSkin, player -> {
  23.                 SimpleForm form = new SimpleForm(npcShop.getName(), "Select an item to buy:");
  24.  
  25.                 npcShop.getItems().forEach((itemKey, shopItem) -> form.addButton(shopItem.item().getName() +
  26.                         " §7- Price: " + shopItem.price() + " " + shopItem.currency().name()));
  27.  
  28.                 form.send(player);
  29.             });
  30.  
  31.             npcs.add(shopNPC); // store strong reference
  32.             level.getChunk(shopPos.getFloorX() >> 4, shopPos.getFloorZ() >> 4, true); // force chunk load
  33.             shopNPC.spawnToAll();
  34.         }
  35.     }
  36.  
  37. public abstract class AbstractNPC extends EntityHuman {
  38.  
  39.     protected final String npcName;
  40.  
  41.     public AbstractNPC(@NotNull Level level, @NotNull Vector3 pos, @NotNull String name, @NotNull Skin skin) {
  42.         super(level.getChunk((int) pos.x >> 4, (int) pos.z >> 4), new CompoundTag()
  43.                 .putList("Pos", new ListTag<DoubleTag>()
  44.                         .add(new DoubleTag(pos.x))
  45.                         .add(new DoubleTag(pos.y))
  46.                         .add(new DoubleTag(pos.z)))
  47.                 .putList("Motion", new ListTag<DoubleTag>()
  48.                         .add(new DoubleTag(0.0))
  49.                         .add(new DoubleTag(0.0))
  50.                         .add(new DoubleTag(0.0)))
  51.                 .putList("Rotation", new ListTag<FloatTag>()
  52.                         .add(new FloatTag(0f))
  53.                         .add(new FloatTag(0f)))
  54.                 .putCompound("Skin", new CompoundTag()) // empty skin
  55.                 .putString("NameTag", name)
  56.         );
  57.         this.setSkin(skin); // must be called BEFORE spawn
  58.         this.npcName = name;
  59.         this.setNameTag(name);
  60.         this.setNameTagVisible(true);
  61.         this.setNameTagAlwaysVisible(true);
  62.         this.setPosition(pos);
  63.     }
  64.  
  65.     /**
  66.      * Called when a player interacts with this NPC
  67.      * Implement in subclasses to open shop/upgrade menus
  68.      */
  69.     public abstract void handleInteraction(@NotNull Player player);
  70.  
  71.     /**
  72.      * Spawns this NPC to all players in the level
  73.      */
  74.     public void spawnToAll() {
  75.         level.loadChunk(getPosition().getFloorX() >> 4, getPosition().getFloorZ() >> 4);
  76.         for (Player p : level.getPlayers().values()) {
  77.             this.spawnTo(p);
  78.         }
  79.     }
  80. }
  81.  
  82. public class CallbackNPC extends AbstractNPC {
  83.     private final Consumer<Player> onInteract;
  84.  
  85.     public CallbackNPC(Level level, Vector3 pos, String name, Skin skin, Consumer<Player> onInteract) {
  86.         super(level, pos, name, skin);
  87.         this.onInteract = onInteract;
  88.     }
  89.  
  90.     @Override
  91.     public void handleInteraction(@NotNull Player player) {
  92.         onInteract.accept(player);
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment