use of org.bukkit.entity.Player in project Denizen-For-Bukkit by DenizenScript.
the class ExecuteCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element cmd = scriptEntry.getElement("command");
Element type = scriptEntry.getElement("type");
Element silent = scriptEntry.getElement("silent");
// Report to dB
dB.report(scriptEntry, getName(), type.debug() + cmd.debug() + silent.debug());
String command = cmd.asString();
switch(Type.valueOf(type.asString())) {
case AS_PLAYER:
try {
PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity(), "/" + command);
Bukkit.getPluginManager().callEvent(pcpe);
if (!pcpe.isCancelled()) {
boolean silentBool = silent.asBoolean();
Player player = ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity();
if (silentBool) {
silencedPlayers.add(player.getUniqueId());
}
player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
if (silentBool) {
silencedPlayers.remove(player.getUniqueId());
}
}
} catch (Throwable e) {
dB.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as player.");
dB.echoError(scriptEntry.getResidingQueue(), e);
}
break;
case AS_OP:
if (CoreUtilities.toLowerCase(command).equals("stop")) {
dB.echoError("Please use as_server to execute 'stop'.");
return;
}
Player player = ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity();
PlayerHelper playerHelper = NMSHandler.getInstance().getPlayerHelper();
boolean isOp = player.isOp();
if (!isOp) {
playerHelper.setTemporaryOp(player, true);
}
try {
PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(player, "/" + command);
Bukkit.getPluginManager().callEvent(pcpe);
if (!pcpe.isCancelled()) {
boolean silentBool = silent.asBoolean();
if (silentBool) {
silencedPlayers.add(player.getUniqueId());
}
player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
if (silentBool) {
silencedPlayers.remove(player.getUniqueId());
}
}
} catch (Throwable e) {
dB.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as OP.");
dB.echoError(scriptEntry.getResidingQueue(), e);
}
if (!isOp) {
playerHelper.setTemporaryOp(player, false);
}
break;
case AS_NPC:
if (!((BukkitScriptEntryData) scriptEntry.entryData).getNPC().isSpawned()) {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot EXECUTE AS_NPC unless the NPC is Spawned.");
return;
}
if (((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity().getType() != EntityType.PLAYER) {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot EXECUTE AS_NPC unless the NPC is Player-Type.");
return;
}
((Player) ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity()).setOp(true);
try {
((Player) ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity()).performCommand(command);
} catch (Throwable e) {
dB.echoError(scriptEntry.getResidingQueue(), "Exception while executing command as NPC-OP.");
dB.echoError(scriptEntry.getResidingQueue(), e);
}
((Player) ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getEntity()).setOp(false);
break;
case AS_SERVER:
dcs.clearOutput();
dcs.silent = silent.asBoolean();
ServerCommandEvent sce = new ServerCommandEvent(dcs, command);
Bukkit.getPluginManager().callEvent(sce);
DenizenAPI.getCurrentInstance().getServer().dispatchCommand(dcs, sce.getCommand());
scriptEntry.addObject("output", new dList(dcs.getOutput()));
break;
}
}
use of org.bukkit.entity.Player in project Denizen-For-Bukkit by DenizenScript.
the class AnimateChestCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dLocation location = (dLocation) scriptEntry.getObject("location");
Element action = scriptEntry.getElement("action");
Element sound = scriptEntry.getElement("sound");
List<dPlayer> players = (List<dPlayer>) scriptEntry.getObject("players");
dB.report(scriptEntry, getName(), location.debug() + action.debug() + sound.debug() + aH.debugObj("players", players.toString()));
PacketHelper packetHelper = NMSHandler.getInstance().getPacketHelper();
switch(ChestAction.valueOf(action.asString().toUpperCase())) {
case OPEN:
for (dPlayer player : players) {
Player ent = player.getPlayerEntity();
if (sound.asBoolean()) {
ent.playSound(location, NMSHandler.getInstance().getSoundHelper().getChestOpen(), 1, 1);
}
packetHelper.showBlockAction(ent, location, 1, 1);
}
break;
case CLOSE:
for (dPlayer player : players) {
Player ent = player.getPlayerEntity();
if (sound.asBoolean()) {
ent.playSound(location, NMSHandler.getInstance().getSoundHelper().getChestClose(), 1, 1);
}
packetHelper.showBlockAction(ent, location, 1, 0);
}
break;
}
}
use of org.bukkit.entity.Player in project Denizen-For-Bukkit by DenizenScript.
the class ViewerCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
dB.echoError(scriptEntry.getResidingQueue(), "This command is deprecated! If you use this, please " + "contact Morphan1 or mcmonkey on irc.esper.net#denizen-dev");
// Get objects
String direction = scriptEntry.hasObject("direction") ? ((Element) scriptEntry.getObject("direction")).asString() : null;
Action action = (Action) scriptEntry.getObject("action");
Type type = scriptEntry.hasObject("type") ? (Type) scriptEntry.getObject("type") : null;
Display display = scriptEntry.hasObject("display") ? (Display) scriptEntry.getObject("display") : null;
final String id = scriptEntry.getObject("id").toString();
if (viewers.containsKey(id)) {
((BukkitScriptEntryData) scriptEntry.entryData).setPlayer(dPlayer.valueOf(viewers.get(id).getContent().split("; ")[1]));
}
dLocation location = scriptEntry.hasObject("location") ? (dLocation) scriptEntry.getObject("location") : null;
String content = scriptEntry.hasObject("display") ? display.toString() + "; " + ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getOfflinePlayer().getUniqueId() : null;
switch(action) {
case CREATE:
if (viewers.containsKey(id)) {
dB.echoDebug(scriptEntry, "Viewer ID " + id + " already exists!");
return;
}
Viewer viewer = new Viewer(id, content, location);
viewers.put(id, viewer);
final Block sign = location.getBlock();
sign.setType(Material.valueOf(type.name()));
if (direction != null) {
Utilities.setSignRotation(sign.getState(), direction);
} else {
Utilities.setSignRotation(sign.getState());
}
int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(), new Runnable() {
public void run() {
Player player = Bukkit.getPlayer(UUID.fromString(viewers.get(id).getContent().split("; ")[1]));
if (player == null) {
Utilities.setSignLines((Sign) viewers.get(id).getLocation().getBlock().getState(), new String[] { "", viewers.get(id).getContent().split("; ")[1], "is offline.", "" });
} else {
Utilities.setSignLines((Sign) viewers.get(id).getLocation().getBlock().getState(), new String[] { String.valueOf((int) player.getLocation().getX()), String.valueOf((int) player.getLocation().getY()), String.valueOf((int) player.getLocation().getZ()), player.getWorld().getName() });
}
}
}, 0, 20);
viewer.setTask(task);
viewer.save();
break;
case MODIFY:
if (!viewers.containsKey(id)) {
dB.echoDebug(scriptEntry, "Viewer ID " + id + " doesn't exist!");
return;
}
if (content != null) {
viewers.get(id).setContent(content);
}
if (location != null) {
if (type == null) {
type = Type.valueOf(viewers.get(id).getLocation().getBlock().getType().name());
}
Bukkit.getScheduler().cancelTask(viewers.get(id).getTask());
int newTask = Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(), new Runnable() {
public void run() {
Player player = Bukkit.getPlayer(UUID.fromString(viewers.get(id).getContent().split("; ")[1]));
if (player == null) {
Utilities.setSignLines((Sign) viewers.get(id).getLocation().getBlock().getState(), new String[] { "", viewers.get(id).getContent().split("; ")[1], "is offline.", "" });
} else {
Utilities.setSignLines((Sign) viewers.get(id).getLocation().getBlock().getState(), new String[] { String.valueOf((int) player.getLocation().getX()), String.valueOf((int) player.getLocation().getY()), String.valueOf((int) player.getLocation().getZ()), player.getWorld().getName() });
}
}
}, 0, 20);
viewers.get(id).getLocation().getBlock().setType(Material.AIR);
viewers.get(id).setLocation(location);
viewers.get(id).setTask(newTask);
location.getBlock().setType(Material.valueOf(type.name()));
}
break;
case REMOVE:
if (!viewers.containsKey(id)) {
dB.echoDebug(scriptEntry, "Viewer ID " + id + " doesn't exist!");
return;
}
Block block = viewers.get(id).getLocation().getBlock();
block.setType(Material.AIR);
Bukkit.getScheduler().cancelTask(viewers.get(id).getTask());
viewers.get(id).remove();
viewers.remove(id);
}
}
use of org.bukkit.entity.Player in project Denizen-For-Bukkit by DenizenScript.
the class PlayEffectCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Extract objects from ScriptEntry
List<dLocation> locations = (List<dLocation>) scriptEntry.getObject("location");
List<dPlayer> targets = (List<dPlayer>) scriptEntry.getObject("targets");
Effect effect = (Effect) scriptEntry.getObject("effect");
Particle particleEffect = (Particle) scriptEntry.getObject("particleeffect");
Element iconcrack = scriptEntry.getElement("iconcrack");
Element iconcrack_data = scriptEntry.getElement("iconcrack_data");
Element iconcrack_type = scriptEntry.getElement("iconcrack_type");
Element radius = scriptEntry.getElement("radius");
Element data = scriptEntry.getElement("data");
Element qty = scriptEntry.getElement("qty");
dLocation offset = scriptEntry.getdObject("offset");
// Report to dB
dB.report(scriptEntry, getName(), (effect != null ? aH.debugObj("effect", effect.getName()) : particleEffect != null ? aH.debugObj("special effect", particleEffect.getName()) : iconcrack_type.debug() + iconcrack.debug() + (iconcrack_data != null ? iconcrack_data.debug() : "")) + aH.debugObj("locations", locations.toString()) + (targets != null ? aH.debugObj("targets", targets.toString()) : "") + radius.debug() + data.debug() + qty.debug() + offset.debug());
for (dLocation location : locations) {
// Slightly increase the location's Y so effects don't seem to come out of the ground
location.add(0, 1, 0);
// Play the Bukkit effect the number of times specified
if (effect != null) {
for (int n = 0; n < qty.asInt(); n++) {
if (targets != null) {
for (dPlayer player : targets) {
if (player.isValid() && player.isOnline()) {
effect.playFor(player.getPlayerEntity(), location, data.asInt());
}
}
} else {
effect.play(location, data.asInt(), radius.asInt());
}
}
} else // Play a ParticleEffect
if (particleEffect != null) {
float osX = (float) offset.getX();
float osY = (float) offset.getY();
float osZ = (float) offset.getZ();
List<Player> players = new ArrayList<Player>();
if (targets == null) {
float rad = radius.asFloat();
for (Player player : location.getWorld().getPlayers()) {
if (player.getLocation().distanceSquared(location) < rad * rad) {
players.add(player);
}
}
} else {
for (dPlayer player : targets) {
if (player.isValid() && player.isOnline()) {
players.add(player.getPlayerEntity());
}
}
}
for (Player player : players) {
particleEffect.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat());
}
} else // Play an iconcrack (item break) effect
{
List<Player> players = new ArrayList<Player>();
if (targets == null) {
float rad = radius.asFloat();
for (Player player : location.getWorld().getPlayers()) {
if (player.getLocation().distanceSquared(location) < rad * rad) {
players.add(player);
}
}
} else {
for (dPlayer player : targets) {
if (player.isValid() && player.isOnline()) {
players.add(player.getPlayerEntity());
}
}
}
// TODO: better this all
if (iconcrack_type.asString().equalsIgnoreCase("iconcrack")) {
ItemStack itemStack = new ItemStack(iconcrack.asInt(), 1, (short) (iconcrack_data != null ? iconcrack_data.asInt() : 0));
Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("ITEM_CRACK");
for (Player player : players) {
particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), itemStack);
}
} else if (iconcrack_type.asString().equalsIgnoreCase("blockcrack")) {
MaterialData materialData = new MaterialData(iconcrack.asInt(), (byte) (iconcrack_data != null ? iconcrack_data.asInt() : 0));
Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("BLOCK_CRACK");
for (Player player : players) {
particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), materialData);
}
} else {
// blockdust
MaterialData materialData = new MaterialData(iconcrack.asInt(), (byte) (iconcrack_data != null ? iconcrack_data.asInt() : 0));
Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("BLOCK_DUST");
for (Player player : players) {
particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), materialData);
}
}
}
}
}
use of org.bukkit.entity.Player in project Denizen-For-Bukkit by DenizenScript.
the class SwitchCommand method switchBlock.
// Break off this portion of the code from execute() so it can be used in both execute and the delayed runnable
public void switchBlock(ScriptEntry scriptEntry, Location interactLocation, SwitchState switchState, Player player) {
World world = interactLocation.getWorld();
boolean currentState = (interactLocation.getBlock().getData() & 0x8) > 0;
String state = switchState.toString();
// Try for a linked player
if (player == null && Bukkit.getOnlinePlayers().size() > 0) {
// If there's none, link any player
if (Bukkit.getOnlinePlayers().size() > 0) {
player = (Player) Bukkit.getOnlinePlayers().toArray()[0];
} else if (Depends.citizens != null) {
// If there are no players, link any Human NPC
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.isSpawned() && npc.getEntity() instanceof Player) {
player = (Player) npc.getEntity();
break;
}
}
// TODO: backup if no human NPC available? (Fake EntityPlayer instance?)
}
}
if ((state.equals("ON") && !currentState) || (state.equals("OFF") && currentState) || state.equals("TOGGLE")) {
try {
if (interactLocation.getBlock().getType() == Material.IRON_DOOR_BLOCK) {
Location block;
if (interactLocation.clone().add(0, -1, 0).getBlock().getType() == Material.IRON_DOOR_BLOCK) {
block = interactLocation.clone().add(0, -1, 0);
} else {
block = interactLocation;
}
block.getBlock().setData((byte) (block.getBlock().getData() ^ 4));
} else {
NMSHandler.getInstance().getEntityHelper().forceInteraction(player, interactLocation);
}
dB.echoDebug(scriptEntry, "Switched " + interactLocation.getBlock().getType().toString() + "! Current state now: " + ((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));
} catch (NullPointerException e) {
dB.echoError("Cannot switch " + interactLocation.getBlock().getType().toString() + "!");
}
}
}
Aggregations