Switch back to sending scoreboards on change only

This commit is contained in:
fullwall 2022-07-27 03:27:49 +08:00
parent 2b88eb7aaa
commit d888d13955
6 changed files with 156 additions and 12 deletions

View File

@ -361,6 +361,9 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
speechFactory.register(Chat.class, "chat");
Bukkit.getPluginManager().registerEvents(new EventListen(storedRegistries), this);
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
new CitizensPlaceholders(selector).register();
}
setupEconomy();

View File

@ -0,0 +1,51 @@
package net.citizensnpcs;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.citizensnpcs.npc.NPCSelector;
public class CitizensPlaceholders extends PlaceholderExpansion {
private final NPCSelector selector;
public CitizensPlaceholders(NPCSelector selector) {
this.selector = selector;
}
@Override
public String getAuthor() {
return "fullwall";
}
@Override
public String getIdentifier() {
return "citizensplaceholder";
}
@Override
public String getVersion() {
return "1.0.0";
}
@Override
public String onRequest(OfflinePlayer player, String params) {
if (player == null || !player.isOnline())
return null;
if (params.equalsIgnoreCase("citizens_selected_npc_name")) {
return selector.getSelected((CommandSender) player).getName();
}
if (params.equalsIgnoreCase("citizens_selected_npc_id")) {
return Integer.toString(selector.getSelected((CommandSender) player).getId());
}
return null;
}
@Override
public boolean persist() {
return true;
}
}

View File

@ -38,6 +38,7 @@ public class NPCSelector implements Listener, net.citizensnpcs.api.npc.NPCSelect
public NPCSelector(Plugin plugin) {
this.plugin = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
}
@Override
@ -116,6 +117,7 @@ public class NPCSelector implements Listener, net.citizensnpcs.api.npc.NPCSelect
}
}
@Override
public void select(CommandSender sender, NPC npc) {
// Remove existing selection if any
List<String> selectors = npc.data().get("selectors");

View File

@ -208,14 +208,16 @@ public class ScoreboardTrait extends Trait {
}
}
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.hasMetadata("NPC"))
continue;
if (SENT_TEAMS.containsEntry(player.getUniqueId(), team.getName())) {
NMS.sendTeamPacket(player, team, 2);
} else {
NMS.sendTeamPacket(player, team, 0);
SENT_TEAMS.put(player.getUniqueId(), team.getName());
if (changed) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.hasMetadata("NPC"))
continue;
if (SENT_TEAMS.containsEntry(player.getUniqueId(), team.getName())) {
NMS.sendTeamPacket(player, team, 2);
} else {
NMS.sendTeamPacket(player, team, 0);
SENT_TEAMS.put(player.getUniqueId(), team.getName());
}
}
}
}

View File

@ -309,6 +309,7 @@ public class ShopTrait extends Trait {
ItemStack item = template.createMenuItem();
if (item == null)
continue;
costItems.getSlots().get(pos).clear();
costItems.getSlots().get(pos).setItemStack(item);
costItems.getSlots().get(pos).addClickHandler(event -> {
event.setCancelled(true);
@ -318,6 +319,7 @@ public class ShopTrait extends Trait {
cost -> modified.changeCost(template::manages, cost)));
});
actionItems.getSlots().get(pos).clear();
actionItems.getSlots().get(pos).setItemStack(item);
actionItems.getSlots().get(pos).addClickHandler(event -> {
event.setCancelled(true);
@ -468,6 +470,7 @@ public class ShopTrait extends Trait {
@Override
public void initialise(MenuContext ctx) {
this.ctx = ctx;
ctx.getSlot(8).clear();
if (shop.pages.size() > 0) {
ctx.getSlot(8).setItemStack(new ItemStack(Material.CHEST, 1), "Open shop");
ctx.getSlot(8).addClickHandler(evt -> {

View File

@ -11,6 +11,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
@ -20,6 +21,8 @@ import com.google.common.collect.Lists;
import net.citizensnpcs.api.gui.InputMenus;
import net.citizensnpcs.api.gui.InventoryMenuPage;
import net.citizensnpcs.api.gui.InventoryMenuSlot;
import net.citizensnpcs.api.gui.Menu;
import net.citizensnpcs.api.gui.MenuContext;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.persistence.PersistenceLoader;
@ -97,10 +100,52 @@ public abstract class NPCShopAction implements Cloneable {
});
}
@Menu(title = "Item editor", dimensions = { 3, 9 })
public static class ItemActionEditor extends InventoryMenuPage {
private ItemAction base;
private Consumer<NPCShopAction> callback;
private MenuContext ctx;
public ItemActionEditor() {
}
public ItemActionEditor(ItemAction base, Consumer<NPCShopAction> callback) {
this.base = base;
this.callback = callback;
}
@Override
public void initialise(MenuContext ctx) {
this.ctx = ctx;
for (int i = 0; i < 3 * 9; i++) {
InventoryMenuSlot slot = ctx.getSlot(i);
slot.clear();
if (i < base.items.size()) {
slot.setItemStack(base.items.get(i).clone());
}
slot.addClickHandler(event -> {
event.setCancelled(true);
event.setCurrentItem(event.getCursorNonNull());
});
}
}
@Override
public void onClose(HumanEntity player) {
List<ItemStack> items = Lists.newArrayList();
for (int i = 0; i < 3 * 9; i++) {
if (ctx.getSlot(i).getCurrentItem() != null) {
items.add(ctx.getSlot(i).getCurrentItem().clone());
}
}
callback.accept(items.isEmpty() ? null : new ItemAction(items));
}
}
public static class ItemActionGUI implements GUI {
@Override
public InventoryMenuPage createEditor(NPCShopAction previous, Consumer<NPCShopAction> callback) {
return null;
return new ItemActionEditor(previous == null ? new ItemAction() : null, callback);
}
@Override
@ -248,20 +293,57 @@ public abstract class NPCShopAction implements Cloneable {
});
}
@Menu(title = "Permissions editor", dimensions = { 3, 9 })
public static class PermissionActionEditor extends InventoryMenuPage {
private NPCShopAction base;
private PermissionAction base;
private Consumer<NPCShopAction> callback;
public PermissionActionEditor() {
}
public PermissionActionEditor(NPCShopAction base, Consumer<NPCShopAction> callback) {
public PermissionActionEditor(PermissionAction base, Consumer<NPCShopAction> callback) {
this.base = base;
this.callback = callback;
}
@Override
public void initialise(MenuContext ctx) {
for (int i = 0; i < 3 * 9; i++) {
final int idx = i;
ctx.getSlot(i).clear();
if (i < base.permissions.size()) {
ctx.getSlot(i).setItemStack(new ItemStack(Material.FEATHER), "<f>Set permission",
"Right click to remove\nCurrently: " + base.permissions.get(i));
}
ctx.getSlot(i).addClickHandler(event -> {
if (event.isRightClick()) {
if (idx < base.permissions.size()) {
base.permissions.remove(idx);
ctx.getSlot(idx).setItemStack(null);
}
return;
}
ctx.getMenu().transition(InputMenus.stringSetter(
() -> idx < base.permissions.size() ? base.permissions.get(idx) : "", (res) -> {
if (res == null) {
if (idx < base.permissions.size()) {
base.permissions.remove(idx);
}
return;
}
if (idx < base.permissions.size()) {
base.permissions.set(idx, res);
} else {
base.permissions.add(res);
}
}));
});
}
}
@Override
public void onClose(HumanEntity player) {
callback.accept(base.permissions.isEmpty() ? null : base);
}
}
@ -270,7 +352,8 @@ public abstract class NPCShopAction implements Cloneable {
@Override
public InventoryMenuPage createEditor(NPCShopAction previous, Consumer<NPCShopAction> callback) {
return new PermissionActionEditor(previous, callback);
return new PermissionActionEditor(
previous == null ? new PermissionAction() : (PermissionAction) previous, callback);
}
@Override