use of de.dytanic.cloudnet.lib.serverselectors.mob.ServerMob in project CloudNet by Dytanic.
the class PacketInMobSelector method handleInput.
@Override
public void handleInput(Document data, PacketSender packetSender) {
Map<UUID, ServerMob> mobMap = data.getObject("mobs", new TypeToken<Map<UUID, ServerMob>>() {
}.getType());
MobConfig mobConfig = data.getObject("mobConfig", new TypeToken<MobConfig>() {
}.getType());
Map<UUID, ServerMob> filteredMobs = MapWrapper.filter(mobMap, new Acceptable<ServerMob>() {
@Override
public boolean isAccepted(ServerMob value) {
return value.getPosition().getGroup().equalsIgnoreCase(CloudAPI.getInstance().getGroup());
}
});
if (MobSelector.getInstance() != null) {
Bukkit.getScheduler().runTask(CloudServer.getInstance().getPlugin(), new Runnable() {
@Override
public void run() {
MobSelector.getInstance().shutdown();
MobSelector.getInstance().setMobConfig(mobConfig);
MobSelector.getInstance().setMobs(new HashMap<>());
MobSelector.getInstance().setMobs(MapWrapper.transform(filteredMobs, new Catcher<UUID, UUID>() {
@Override
public UUID doCatch(UUID key) {
return key;
}
}, new Catcher<MobSelector.MobImpl, ServerMob>() {
@Override
public MobSelector.MobImpl doCatch(ServerMob key) {
MobSelector.getInstance().toLocation(key.getPosition()).getChunk().load();
Entity entity = MobSelector.getInstance().toLocation(key.getPosition()).getWorld().spawnEntity(MobSelector.getInstance().toLocation(key.getPosition()), EntityType.valueOf(key.getType()));
entity.setFireTicks(0);
Object armorStand = ReflectionUtil.armorstandCreation(MobSelector.getInstance().toLocation(key.getPosition()), entity, key);
if (armorStand != null) {
MobSelector.getInstance().updateCustom(key, armorStand);
Entity armor = (Entity) armorStand;
if (armor.getPassenger() == null && key.getItemId() != null) {
Item item = Bukkit.getWorld(key.getPosition().getWorld()).dropItem(armor.getLocation(), new ItemStack(key.getItemId()));
item.setTicksLived(Integer.MAX_VALUE);
item.setPickupDelay(Integer.MAX_VALUE);
armor.setPassenger(item);
}
}
if (entity instanceof Villager) {
((Villager) entity).setProfession(Villager.Profession.FARMER);
}
MobSelector.getInstance().unstableEntity(entity);
entity.setCustomNameVisible(true);
entity.setCustomName(ChatColor.translateAlternateColorCodes('&', key.getDisplay()));
MobSelector.MobImpl mob = new MobSelector.MobImpl(key.getUniqueId(), key, entity, MobSelector.getInstance().create(mobConfig, key), new HashMap<>(), armorStand);
Bukkit.getPluginManager().callEvent(new BukkitMobInitEvent(mob));
return mob;
}
}));
Bukkit.getScheduler().runTaskAsynchronously(CloudServer.getInstance().getPlugin(), new Runnable() {
@Override
public void run() {
for (ServerInfo serverInfo : MobSelector.getInstance().getServers().values()) {
MobSelector.getInstance().handleUpdate(serverInfo);
}
}
});
}
});
} else {
MobSelector mobSelector = new MobSelector(mobConfig);
MobSelector.getInstance().setMobs(new HashMap<>());
Bukkit.getScheduler().runTask(CloudServer.getInstance().getPlugin(), new Runnable() {
@Override
public void run() {
MobSelector.getInstance().setMobs(MapWrapper.transform(filteredMobs, new Catcher<UUID, UUID>() {
@Override
public UUID doCatch(UUID key) {
return key;
}
}, new Catcher<MobSelector.MobImpl, ServerMob>() {
@Override
public MobSelector.MobImpl doCatch(ServerMob key) {
MobSelector.getInstance().toLocation(key.getPosition()).getChunk().load();
Entity entity = MobSelector.getInstance().toLocation(key.getPosition()).getWorld().spawnEntity(MobSelector.getInstance().toLocation(key.getPosition()), EntityType.valueOf(key.getType()));
Object armorStand = ReflectionUtil.armorstandCreation(MobSelector.getInstance().toLocation(key.getPosition()), entity, key);
if (armorStand != null) {
MobSelector.getInstance().updateCustom(key, armorStand);
Entity armor = (Entity) armorStand;
if (armor.getPassenger() == null && key.getItemId() != null) {
Item item = Bukkit.getWorld(key.getPosition().getWorld()).dropItem(armor.getLocation(), new ItemStack(key.getItemId()));
item.setTicksLived(Integer.MAX_VALUE);
item.setPickupDelay(Integer.MAX_VALUE);
armor.setPassenger(item);
}
}
if (entity instanceof Villager) {
((Villager) entity).setProfession(Villager.Profession.FARMER);
}
MobSelector.getInstance().unstableEntity(entity);
entity.setCustomNameVisible(true);
entity.setCustomName(ChatColor.translateAlternateColorCodes('&', key.getDisplay()));
MobSelector.MobImpl mob = new MobSelector.MobImpl(key.getUniqueId(), key, entity, MobSelector.getInstance().create(mobConfig, key), new HashMap<>(), armorStand);
Bukkit.getPluginManager().callEvent(new BukkitMobInitEvent(mob));
return mob;
}
}));
}
});
mobSelector.init();
}
}
use of de.dytanic.cloudnet.lib.serverselectors.mob.ServerMob in project CloudNet by Dytanic.
the class CommandCloudServer method onCommand.
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
if (!(commandSender instanceof Player))
return false;
Player player = (Player) commandSender;
if (args.length > 5) {
if (args[0].equalsIgnoreCase("createMob")) {
if (MobSelector.getInstance() == null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The Module \"MobSelector\" isn't enabled!");
return false;
}
try {
EntityType entityType = EntityType.valueOf(args[1].toUpperCase());
if (!entityType.isAlive() || !entityType.isSpawnable())
return false;
if (CollectionWrapper.filter(MobSelector.getInstance().getMobs().values(), new Acceptable<MobSelector.MobImpl>() {
@Override
public boolean isAccepted(MobSelector.MobImpl value) {
return value.getMob().getName().equalsIgnoreCase(args[2]);
}
}) == null) {
StringBuilder stringBuilder = new StringBuilder();
for (short i = 6; i < args.length; i++) {
stringBuilder.append(args[i]).append(NetworkUtils.SPACE_STRING);
}
if (stringBuilder.length() > 32) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The display cannot be longe then 32 characters");
return false;
}
ServerMob serverMob = new ServerMob(UUID.randomUUID(), stringBuilder.substring(0, stringBuilder.length() - 1), args[2], entityType.name(), args[3], NetworkUtils.checkIsNumber(args[4]) ? (Integer.parseInt(args[4]) != 0 ? Integer.parseInt(args[4]) : 138) : 138, args[5].equalsIgnoreCase("true"), MobSelector.getInstance().toPosition(CloudAPI.getInstance().getGroup(), player.getLocation()), "§8#§c%group% &bPlayers online §8|§7 %group_online% of %max_players%", new Document());
CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutAddMob(serverMob));
player.sendMessage(CloudAPI.getInstance().getPrefix() + "The mob will be created, please wait...");
} else {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The mob with the name " + args[2] + " already exists!");
return false;
}
} catch (Exception ex) {
for (EntityType entityType : EntityType.values()) commandSender.sendMessage("- " + entityType.name());
}
}
}
if (args.length > 2) {
if (args[0].equalsIgnoreCase("editMobLine")) {
if (MobSelector.getInstance() == null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The Module \"MobSelector\" isn't enabled!");
return false;
}
MobSelector.MobImpl mob = CollectionWrapper.filter(MobSelector.getInstance().getMobs().values(), new Acceptable<MobSelector.MobImpl>() {
@Override
public boolean isAccepted(MobSelector.MobImpl value) {
return value.getMob().getName().equalsIgnoreCase(args[1]);
}
});
if (mob != null) {
StringBuilder stringBuilder = new StringBuilder();
for (short i = 2; i < args.length; i++) stringBuilder.append(args[i]).append(NetworkUtils.SPACE_STRING);
mob.getMob().setDisplayMessage(stringBuilder.substring(0, stringBuilder.length() - 1));
CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutAddMob(mob.getMob()));
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "You set the mobline for \"" + args[1] + "\" the line \"" + stringBuilder.substring(0, stringBuilder.length() - 1) + "\"");
return false;
}
}
if (args[0].equalsIgnoreCase("setDisplay")) {
MobSelector.MobImpl mob = CollectionWrapper.filter(MobSelector.getInstance().getMobs().values(), new Acceptable<MobSelector.MobImpl>() {
@Override
public boolean isAccepted(MobSelector.MobImpl value) {
return value.getMob().getName().equalsIgnoreCase(args[1]);
}
});
if (mob != null) {
StringBuilder stringBuilder = new StringBuilder();
for (short i = 2; i < args.length; i++) stringBuilder.append(args[i]).append(NetworkUtils.SPACE_STRING);
mob.getMob().setDisplay(stringBuilder.substring(0, stringBuilder.length() - 1));
CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutAddMob(mob.getMob()));
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "You set the display for \"" + args[1] + "\" the line \"" + stringBuilder.substring(0, stringBuilder.length() - 1) + "\"");
return false;
}
}
}
switch(args.length) {
case 2:
if (args[0].equalsIgnoreCase("createSign")) {
if (SignSelector.getInstance() == null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The Module \"SignSelector\" isn't enabled!");
return false;
}
Block block = player.getTargetBlock((HashSet<Material>) null, 15);
if (block.getState() instanceof org.bukkit.block.Sign)
if (!SignSelector.getInstance().containsPosition(block.getLocation())) {
if (CloudAPI.getInstance().getServerGroupMap().containsKey(args[1])) {
Sign sign = new Sign(args[1], SignSelector.getInstance().toPosition(block.getLocation()));
CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutAddSign(sign));
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The sign was successfully created!");
} else
commandSender.sendMessage("The group doesn't exist");
} else
commandSender.sendMessage("The sign already exists!");
return false;
}
if (args[0].equalsIgnoreCase("removeSigns")) {
if (SignSelector.getInstance() == null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The Module \"SignSelector\" isn't enabled!");
return false;
}
for (Sign sign : SignSelector.getInstance().getSigns().values()) if (sign.getTargetGroup() != null && sign.getTargetGroup().equalsIgnoreCase(args[1]))
CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutRemoveSign(sign));
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "§7You deleted all signs from the group \"§6" + args[1] + "§7\"");
}
if (args[0].equalsIgnoreCase("removeMob")) {
if (MobSelector.getInstance() == null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The Module \"MobSelector\" isn't enabled!");
return false;
}
MobSelector.MobImpl mob = CollectionWrapper.filter(MobSelector.getInstance().getMobs().values(), new Acceptable<MobSelector.MobImpl>() {
@Override
public boolean isAccepted(MobSelector.MobImpl value) {
return value.getMob().getName().equalsIgnoreCase(args[1]);
}
});
if (mob != null) {
CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutRemoveMob(mob.getMob()));
player.sendMessage(CloudAPI.getInstance().getPrefix() + "The mob has been removed");
} else
player.sendMessage(CloudAPI.getInstance().getPrefix() + "The Mob doesn't exist on this group");
}
break;
case 1:
if (args[0].equalsIgnoreCase("removeSign")) {
if (SignSelector.getInstance() == null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The Module \"SignSelector\" isn't enabled!");
return false;
}
Block block = player.getTargetBlock((HashSet<Byte>) null, 15);
if (block.getState() instanceof org.bukkit.block.Sign) {
if (SignSelector.getInstance().containsPosition(block.getLocation())) {
Sign sign = SignSelector.getInstance().getSignByPosition(block.getLocation());
if (sign != null) {
CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutRemoveSign(sign));
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The sign has been removed");
}
}
}
}
if (args[0].equalsIgnoreCase("listMobs")) {
if (MobSelector.getInstance() == null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The Module \"MobSelector\" isn't enabled!");
return false;
}
CollectionWrapper.iterator(MobSelector.getInstance().getMobs().values(), new Runnabled<MobSelector.MobImpl>() {
@Override
public void run(MobSelector.MobImpl obj) {
commandSender.sendMessage("- " + obj.getMob().getName());
}
});
}
if (args[0].equalsIgnoreCase("moblist")) {
if (MobSelector.getInstance() == null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The Module \"MobSelector\" isn't enabled!");
return false;
}
for (EntityType entityType : EntityType.values()) {
if (entityType.isAlive() && entityType.isSpawnable())
commandSender.sendMessage("- " + entityType.name());
}
}
break;
case 3:
if (args[0].equalsIgnoreCase("setItem")) {
if (MobSelector.getInstance() == null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The Module \"MobSelector\" isn't enabled!");
return false;
}
MobSelector.MobImpl mob = CollectionWrapper.filter(MobSelector.getInstance().getMobs().values(), new Acceptable<MobSelector.MobImpl>() {
@Override
public boolean isAccepted(MobSelector.MobImpl value) {
return value.getMob().getName().equalsIgnoreCase(args[1]);
}
});
if (mob != null) {
int itemId = NetworkUtils.checkIsNumber(args[2]) ? Integer.parseInt(args[2]) : 138;
mob.getMob().setItemId(itemId);
CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutAddMob(mob.getMob()));
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "You set the item for \"" + args[1] + "\" the itemId \"" + itemId + "\"");
return false;
}
}
break;
default:
if (SignSelector.getInstance() != null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver createSign <targetGroup>");
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver removeSign");
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver removeSigns <targetGroup>");
}
if (MobSelector.getInstance() != null) {
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver createMob <entityType> <name> <targetGroup> <itemId> <autoJoin> <displayName>");
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver removeMob <name>");
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver listMobs");
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver moblist");
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver setDisplay <name> <display>");
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver setItem <name> <itemId>");
commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "/cloudserver editMobLine <name> <display>");
}
break;
}
return false;
}
use of de.dytanic.cloudnet.lib.serverselectors.mob.ServerMob in project CloudNet by Dytanic.
the class MobDatabase method loadAll.
public Map<UUID, ServerMob> loadAll() {
boolean injectable = false;
Map<UUID, ServerMob> mobMap = database.getDocument("server_selector_mobs").getObject("mobs", new TypeToken<Map<UUID, ServerMob>>() {
}.getType());
for (ServerMob serverMob : mobMap.values()) {
if (serverMob.getItemId() == null) {
serverMob.setItemId(138);
injectable = true;
}
if (serverMob.getAutoJoin() == null) {
serverMob.setAutoJoin(false);
injectable = true;
}
}
if (injectable) {
Document document = database.getDocument("server_selector_mobs");
document.append("mobs", mobMap);
database.insert(document);
}
return mobMap;
}
use of de.dytanic.cloudnet.lib.serverselectors.mob.ServerMob in project CloudNet by Dytanic.
the class PacketInAddMob method handleInput.
@Override
public void handleInput(Document data, PacketSender packetSender) {
ServerMob serverMob = data.getObject("mob", new TypeToken<ServerMob>() {
}.getType());
MobModule.getInstance().getMobDatabase().append(serverMob);
CloudNet.getInstance().getNetworkManager().reload();
CloudNet.getInstance().getNetworkManager().updateAll();
}
use of de.dytanic.cloudnet.lib.serverselectors.mob.ServerMob in project CloudNet by Dytanic.
the class PacketInRemoveMob method handleInput.
@Override
public void handleInput(Document data, PacketSender packetSender) {
ServerMob serverMob = data.getObject("mob", new TypeToken<ServerMob>() {
}.getType());
MobModule.getInstance().getMobDatabase().remove(serverMob);
CloudNet.getInstance().getNetworkManager().reload();
CloudNet.getInstance().getNetworkManager().updateAll();
}
Aggregations