Skip to content

Commit

Permalink
Implement new API
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jan 5, 2023
1 parent d9e7ec4 commit 98c5c48
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 140 deletions.
68 changes: 35 additions & 33 deletions main/src/main/java/net/citizensnpcs/Citizens.java
Expand Up @@ -10,7 +10,10 @@
import java.util.concurrent.Callable;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.inventory.InventoryType;
Expand All @@ -34,9 +37,8 @@
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.CitizensPlugin;
import net.citizensnpcs.api.InventoryHelper;
import net.citizensnpcs.api.LocationLookup;
import net.citizensnpcs.api.SkullMetaProvider;
import net.citizensnpcs.api.NMSHelper;
import net.citizensnpcs.api.ai.speech.SpeechFactory;
import net.citizensnpcs.api.command.CommandManager;
import net.citizensnpcs.api.command.Injector;
Expand Down Expand Up @@ -86,30 +88,15 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
private final CommandManager commands = new CommandManager();
private Settings config;
private boolean enabled;
private final InventoryHelper inventoryHelper = new InventoryHelper() {
@Override
public InventoryView openAnvilInventory(Player player, Inventory inventory, String title) {
return NMS.openAnvilInventory(player, inventory, title);
}
private LocationLookup locationLookup;
private final NMSHelper nmsHelper = new NMSHelper() {
private boolean SUPPORT_OWNER_PROFILE = true;

@Override
public void updateInventoryTitle(Player player, InventoryView view, String newTitle) {
if (view.getTopInventory().getType() == InventoryType.CRAFTING
|| view.getTopInventory().getType() == InventoryType.CREATIVE
|| view.getTopInventory().getType() == InventoryType.PLAYER)
return;
NMS.updateInventoryTitle(player, view, newTitle);
public OfflinePlayer getPlayer(BlockCommandSender sender) {
Entity entity = NMS.getSource(sender);
return entity != null && entity instanceof OfflinePlayer ? (OfflinePlayer) entity : null;
}
};
private LocationLookup locationLookup;
private CitizensNPCRegistry npcRegistry;
private ProtocolLibListener protocolListener;
private boolean saveOnDisable = true;
private NPCDataStore saves;
private NPCSelector selector;
private StoredShops shops;
private final SkullMetaProvider skullMetaProvider = new SkullMetaProvider() {
private boolean SUPPORT_OWNER_PROFILE = true;

@Override
public String getTexture(SkullMeta meta) {
Expand All @@ -118,6 +105,11 @@ public String getTexture(SkullMeta meta) {
: Iterables.getFirst(profile.getProperties().get("textures"), new Property("", "")).getValue();
}

@Override
public InventoryView openAnvilInventory(Player player, Inventory inventory, String title) {
return NMS.openAnvilInventory(player, inventory, title);
}

@Override
public void setTexture(String texture, SkullMeta meta) {
GameProfile profile = NMS.getProfile(meta);
Expand All @@ -137,7 +129,22 @@ public void setTexture(String texture, SkullMeta meta) {
profile.getProperties().put("textures", new Property("textures", texture));
NMS.setProfile(meta, profile);
}

@Override
public void updateInventoryTitle(Player player, InventoryView view, String newTitle) {
if (view.getTopInventory().getType() == InventoryType.CRAFTING
|| view.getTopInventory().getType() == InventoryType.CREATIVE
|| view.getTopInventory().getType() == InventoryType.PLAYER)
return;
NMS.updateInventoryTitle(player, view, newTitle);
}
};
private CitizensNPCRegistry npcRegistry;
private ProtocolLibListener protocolListener;
private boolean saveOnDisable = true;
private NPCDataStore saves;
private NPCSelector selector;
private StoredShops shops;
private CitizensSpeechFactory speechFactory;
private final Map<String, NPCRegistry> storedRegistries = Maps.newHashMap();
private CitizensTraitFactory traitFactory;
Expand Down Expand Up @@ -229,11 +236,6 @@ public net.citizensnpcs.api.npc.NPCSelector getDefaultNPCSelector() {
return selector;
}

@Override
public InventoryHelper getInventoryHelper() {
return inventoryHelper;
}

@Override
public LocationLookup getLocationLookup() {
return locationLookup;
Expand All @@ -246,6 +248,11 @@ public NPCRegistry getNamedNPCRegistry(String name) {
return storedRegistries.get(name);
}

@Override
public NMSHelper getNMSHelper() {
return nmsHelper;
}

@Override
public Iterable<NPCRegistry> getNPCRegistries() {
return new Iterable<NPCRegistry>() {
Expand Down Expand Up @@ -302,11 +309,6 @@ public StoredShops getShops() {
return shops;
}

@Override
public SkullMetaProvider getSkullMetaProvider() {
return skullMetaProvider;
}

@Override
public SpeechFactory getSpeechFactory() {
return speechFactory;
Expand Down
5 changes: 5 additions & 0 deletions main/src/main/java/net/citizensnpcs/util/NMS.java
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.Entity;
import org.bukkit.entity.FishHook;
Expand Down Expand Up @@ -379,6 +380,10 @@ public static String getSound(String flag) throws CommandException {
return BRIDGE.getSound(flag);
}

public static Entity getSource(BlockCommandSender sender) {
return BRIDGE.getSource(sender);
}

public static float getSpeedFor(NPC npc) {
return BRIDGE.getSpeedFor(npc);
}
Expand Down
19 changes: 11 additions & 8 deletions main/src/main/java/net/citizensnpcs/util/NMSBridge.java
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.Entity;
import org.bukkit.entity.FishHook;
Expand Down Expand Up @@ -91,6 +92,8 @@ public default Object getBossBar(Entity entity) {

public String getSound(String flag) throws CommandException;

public Entity getSource(BlockCommandSender sender);

public float getSpeedFor(NPC npc);

public float getStepHeight(Entity entity);
Expand Down Expand Up @@ -146,9 +149,9 @@ public default void onPlayerInfoAdd(Player player, Object source) {

public void removeFromWorld(org.bukkit.entity.Entity entity);

public void removeHookIfNecessary(NPCRegistry npcRegistry, FishHook entity);
public void removeHookIfNecessary(NPCRegistry npcRegistry, FishHook entity);;

public void replaceTrackerEntry(Player player);;
public void replaceTrackerEntry(Player player);

public void sendPositionUpdate(Player excluding, Entity from, Location location);

Expand All @@ -164,25 +167,25 @@ public default void onPlayerInfoAdd(Player player, Object source) {

public default void setAllayDancing(Entity entity, boolean dancing) {
throw new UnsupportedOperationException();
}
};

public void setBodyYaw(Entity entity, float yaw);;
public void setBodyYaw(Entity entity, float yaw);

public void setBoundingBox(Entity entity, BoundingBox box);

public default void setCamelPose(Entity entity, CamelPose pose) {
throw new UnsupportedOperationException();
}
};

public void setCustomName(Entity entity, Object component, String string);;

public void setDestination(Entity entity, double x, double y, double z, float speed);;

public void setDimensions(Entity entity, EntityDim desired);;
public void setDimensions(Entity entity, EntityDim desired);

public void setEndermanAngry(Enderman enderman, boolean angry);
public void setEndermanAngry(Enderman enderman, boolean angry);;

public void setHeadYaw(Entity entity, float yaw);;
public void setHeadYaw(Entity entity, float yaw);

public void setKnockbackResistance(LivingEntity entity, double d);

Expand Down
Expand Up @@ -26,10 +26,12 @@
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.craftbukkit.v1_10_R1.CraftServer;
import org.bukkit.craftbukkit.v1_10_R1.CraftSound;
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_10_R1.boss.CraftBossBar;
import org.bukkit.craftbukkit.v1_10_R1.command.CraftBlockCommandSender;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftWither;
Expand Down Expand Up @@ -336,15 +338,6 @@ public void link(Player player) {
tracker.trackedPlayers.add(p);
}

@Override
public void unlinkAll(Consumer<Player> callback) {
for (EntityPlayer link : Lists.newArrayList(tracker.trackedPlayers)) {
Player entity = link.getBukkitEntity();
unlink(entity);
callback.accept(entity);
}
}

@Override
public void run() {
tracker.a();
Expand All @@ -356,6 +349,15 @@ public void unlink(Player player) {
tracker.a(p);
tracker.trackedPlayers.remove(p);
}

@Override
public void unlinkAll(Consumer<Player> callback) {
for (EntityPlayer link : Lists.newArrayList(tracker.trackedPlayers)) {
Player entity = link.getBukkitEntity();
unlink(entity);
callback.accept(entity);
}
}
};
}

Expand Down Expand Up @@ -534,6 +536,12 @@ public String getSound(String flag) throws CommandException {
}
}

@Override
public org.bukkit.entity.Entity getSource(BlockCommandSender sender) {
Entity source = ((CraftBlockCommandSender) sender).getTileEntity().f();
return source != null ? source.getBukkitEntity() : null;
}

@Override
public float getSpeedFor(NPC npc) {
if (!npc.isSpawned() || !(npc.getEntity() instanceof LivingEntity))
Expand Down
Expand Up @@ -26,10 +26,12 @@
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.craftbukkit.v1_11_R1.CraftServer;
import org.bukkit.craftbukkit.v1_11_R1.CraftSound;
import org.bukkit.craftbukkit.v1_11_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_11_R1.boss.CraftBossBar;
import org.bukkit.craftbukkit.v1_11_R1.command.CraftBlockCommandSender;
import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_11_R1.entity.CraftWither;
Expand Down Expand Up @@ -357,15 +359,6 @@ public void link(Player player) {
tracker.trackedPlayers.add(p);
}

@Override
public void unlinkAll(Consumer<Player> callback) {
for (EntityPlayer link : Lists.newArrayList(tracker.trackedPlayers)) {
Player entity = link.getBukkitEntity();
unlink(entity);
callback.accept(entity);
}
}

@Override
public void run() {
tracker.a();
Expand All @@ -377,6 +370,15 @@ public void unlink(Player player) {
tracker.a(p);
tracker.trackedPlayers.remove(p);
}

@Override
public void unlinkAll(Consumer<Player> callback) {
for (EntityPlayer link : Lists.newArrayList(tracker.trackedPlayers)) {
Player entity = link.getBukkitEntity();
unlink(entity);
callback.accept(entity);
}
}
};
}

Expand Down Expand Up @@ -555,6 +557,12 @@ public String getSound(String flag) throws CommandException {
}
}

@Override
public org.bukkit.entity.Entity getSource(BlockCommandSender sender) {
Entity source = ((CraftBlockCommandSender) sender).getTileEntity().f();
return source != null ? source.getBukkitEntity() : null;
}

@Override
public float getSpeedFor(NPC npc) {
if (!npc.isSpawned() || !(npc.getEntity() instanceof LivingEntity))
Expand Down
Expand Up @@ -26,10 +26,12 @@
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.craftbukkit.v1_12_R1.CraftServer;
import org.bukkit.craftbukkit.v1_12_R1.CraftSound;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_12_R1.boss.CraftBossBar;
import org.bukkit.craftbukkit.v1_12_R1.command.CraftBlockCommandSender;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftWither;
Expand Down Expand Up @@ -362,15 +364,6 @@ public void link(Player player) {
tracker.trackedPlayers.add(p);
}

@Override
public void unlinkAll(Consumer<Player> callback) {
for (EntityPlayer link : Lists.newArrayList(tracker.trackedPlayers)) {
Player entity = link.getBukkitEntity();
unlink(entity);
callback.accept(entity);
}
}

@Override
public void run() {
tracker.a();
Expand All @@ -382,6 +375,15 @@ public void unlink(Player player) {
tracker.a(p);
tracker.trackedPlayers.remove(p);
}

@Override
public void unlinkAll(Consumer<Player> callback) {
for (EntityPlayer link : Lists.newArrayList(tracker.trackedPlayers)) {
Player entity = link.getBukkitEntity();
unlink(entity);
callback.accept(entity);
}
}
};
}

Expand Down Expand Up @@ -554,6 +556,12 @@ public String getSound(String flag) throws CommandException {
}
}

@Override
public org.bukkit.entity.Entity getSource(BlockCommandSender sender) {
Entity source = ((CraftBlockCommandSender) sender).getTileEntity().f();
return source != null ? source.getBukkitEntity() : null;
}

@Override
public float getSpeedFor(NPC npc) {
if (!npc.isSpawned() || !(npc.getEntity() instanceof LivingEntity))
Expand Down

0 comments on commit 98c5c48

Please sign in to comment.