Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #38 from Xenmai/master
Browse files Browse the repository at this point in the history
Explosion event, LookAt command, Entity tags
  • Loading branch information
mcmonkey4eva committed Oct 5, 2017
2 parents ab3e014 + b203922 commit 9969bae
Show file tree
Hide file tree
Showing 6 changed files with 547 additions and 12 deletions.
Expand Up @@ -19,12 +19,10 @@
import com.denizenscript.denizen2sponge.events.entity.*;
import com.denizenscript.denizen2sponge.events.player.*;
import com.denizenscript.denizen2sponge.events.server.ClientPingsServerScriptEvent;
import com.denizenscript.denizen2sponge.events.server.CommandSentScriptEvent;
import com.denizenscript.denizen2sponge.events.server.InternalScriptEvent;
import com.denizenscript.denizen2sponge.events.server.ServerStopsScriptEvent;
import com.denizenscript.denizen2sponge.events.world.BlockChangeScriptEvent;
import com.denizenscript.denizen2sponge.events.world.BlockFadesScriptEvent;
import com.denizenscript.denizen2sponge.events.world.WorldLoadsScriptEvent;
import com.denizenscript.denizen2sponge.events.world.WorldUnloadsScriptEvent;
import com.denizenscript.denizen2sponge.events.world.*;
import com.denizenscript.denizen2sponge.spongecommands.ExCommand;
import com.denizenscript.denizen2sponge.spongeevents.Denizen2SpongeLoadedEvent;
import com.denizenscript.denizen2sponge.spongeevents.Denizen2SpongeLoadingEvent;
Expand Down Expand Up @@ -132,6 +130,7 @@ public void onServerStart(GamePreInitializationEvent event) {
Denizen2Core.register(new FlagCommand());
Denizen2Core.register(new HealCommand());
Denizen2Core.register(new HurtCommand());
Denizen2Core.register(new LookAtCommand());
Denizen2Core.register(new MountCommand());
Denizen2Core.register(new RemoveCommand());
Denizen2Core.register(new SpawnCommand());
Expand Down Expand Up @@ -192,11 +191,13 @@ public void onServerStart(GamePreInitializationEvent event) {
Denizen2Core.register(new PlayerRightClicksScriptEvent());
// Events: Server
Denizen2Core.register(new ClientPingsServerScriptEvent());
Denizen2Core.register(new CommandSentScriptEvent());
Denizen2Core.register(new InternalScriptEvent());
Denizen2Core.register(new ServerStopsScriptEvent());
// Events: World
Denizen2Core.register(new BlockChangeScriptEvent());
Denizen2Core.register(new BlockFadesScriptEvent());
Denizen2Core.register(new ExplosionDetonatesScriptEvent());
Denizen2Core.register(new WorldLoadsScriptEvent());
Denizen2Core.register(new WorldUnloadsScriptEvent());
// Tag Handlers: Sponge Basics
Expand Down
@@ -0,0 +1,61 @@
package com.denizenscript.denizen2sponge.commands.entity;

import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.flowpowered.math.vector.Vector3d;
import org.spongepowered.api.entity.living.Living;

public class LookAtCommand extends AbstractCommand {

// <--[command]
// @Name lookat
// @Arguments <entity> <location>
// @Short makes an entity look at a location.
// @Updated 2017/10/02
// @Group Entity
// @Minimum 2
// @Maximum 2
// @Description
// Makes a living entity look at a location. This will rotate the entity's head, but not its body.
// @Example
// # This example makes the target entity of the player look at him in the eye
// - lookat <player.entities_on_cursor.get[1]> <player.eye_location>
// -->

@Override
public String getName() {
return "lookat";
}

@Override
public String getArguments() {
return "<entity> <location>";
}

@Override
public int getMinimumArguments() {
return 2;
}

@Override
public int getMaximumArguments() {
return 2;
}

@Override
public void execute(CommandQueue queue, CommandEntry entry) {
EntityTag entityTag = EntityTag.getFor(queue.error, entry.getArgumentObject(queue, 0));
Living ent = (Living) entityTag.getInternal();
LocationTag locationTag = LocationTag.getFor(queue.error, entry.getArgumentObject(queue, 1));
Vector3d loc = locationTag.getInternal().toVector3d();
ent.lookAt(loc);
if (queue.shouldShowGood()) {
queue.outGood("Entity " + ColorSet.emphasis + entityTag.debug() + ColorSet.good + " is now looking at "
+ ColorSet.emphasis + locationTag.debug() + ColorSet.good + "!");
}
}
}
@@ -0,0 +1,171 @@
package com.denizenscript.denizen2sponge.events.server;

import com.denizenscript.denizen2core.events.ScriptEvent;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.objects.ListTag;
import com.denizenscript.denizen2core.tags.objects.TextTag;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.tags.objects.PlayerTag;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.tileentity.CommandBlock;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.vehicle.minecart.CommandBlockMinecart;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.command.SendCommandEvent;

import java.util.HashMap;

public class CommandSentScriptEvent extends ScriptEvent {

// <--[event]
// @Events
// command sent
//
// @Updated 2017/10/04
//
// @Cancellable true
//
// @Group Server
//
// @Triggers when a command is used by any source.
//
// @Context
// command (TextTag) returns the main command that was used.
// args (ListTag<TextTag>) returns a list of the arguments used.
// raw_args (TextTag) returns the arguments as a single text tag.
// source (TextTag) returns whether the command was sent by the console, a player or a command block.
// player (PlayerTag) returns the player that sent the command, if the source was in fact one.
// location (LocationTag) returns the location of the command block that sent the command, if the source is in fact one.
// entity (EntityTag) returns the command block minecart that sent the command, if the source is in fact one.
//
// @Determinations
// command (TextTag) to set the main command that will be used.
// args (ListTag<TextTag>) to set a list of the arguments that will be used.
// raw_args (TextTag) to set the arguments as a single text tag.
// -->

@Override
public String getName() {
return "CommandSent";
}

@Override
public boolean couldMatch(ScriptEventData data) {
return data.eventPath.startsWith("command sent");
}

@Override
public boolean matches(ScriptEventData data) {
return true;
}

public TextTag command;

public ListTag args;

public TextTag raw_args;

public TextTag source;

public PlayerTag player;

public LocationTag location;

public EntityTag entity;

public SendCommandEvent internal;

// TODO: Thread safety!

@Override
public HashMap<String, AbstractTagObject> getDefinitions(ScriptEventData data) {
HashMap<String, AbstractTagObject> defs = super.getDefinitions(data);
defs.put("command", command);
defs.put("args", args);
defs.put("raw_args", raw_args);
defs.put("source", source);
defs.put("player", player);
defs.put("location", location);
defs.put("entity", entity);
return defs;
}

@Override
public void enable() {
Sponge.getEventManager().registerListeners(Denizen2Sponge.instance, this);
}

@Override
public void disable() {
Sponge.getEventManager().unregisterListeners(this);
}

@Listener
public void onCommandSent(SendCommandEvent evt) {
CommandSentScriptEvent event = (CommandSentScriptEvent) clone();
event.internal = evt;
event.command = new TextTag(evt.getCommand());
// TODO: Improve splitting (quoted arguments)
ListTag list = new ListTag();
for (String arg : evt.getArguments().split(" ")) {
list.getInternal().add(new TextTag(arg));
}
event.args = list;
event.raw_args = new TextTag(evt.getArguments());
CommandSource source = (CommandSource) evt.getSource();
if (source instanceof Player) {
event.source = new TextTag("player");
event.player = new PlayerTag((Player) source);
}
else if (source instanceof CommandBlock) {
event.source = new TextTag("block");
event.location = new LocationTag(((CommandBlock) source).getLocation());
}
else if (source instanceof CommandBlockMinecart) {
event.source = new TextTag("minecart");
event.entity = new EntityTag((CommandBlockMinecart) source);
}
else {
event.source = new TextTag("server");
}
event.cancelled = evt.isCancelled();
event.run();
evt.setCancelled(event.cancelled);
}

@Override
public void applyDetermination(boolean errors, String determination, AbstractTagObject value) {
if (determination.equals("command")) {
TextTag tt = TextTag.getFor(this::error, value);
command = tt;
internal.setCommand(tt.getInternal());
}
else if (determination.equals("args")) {
ListTag lt = ListTag.getFor(this::error, value);
args = lt;
String string = "";
for (AbstractTagObject arg : lt.getInternal()) {
string += " " + ((TextTag) arg).getInternal();
}
raw_args = new TextTag(string);
internal.setArguments(string);
}
else if (determination.equals("raw_args")) {
TextTag tt = TextTag.getFor(this::error, value);
raw_args = tt;
// TODO: Improve splitting (quoted arguments)
ListTag list = new ListTag();
for (String arg : tt.getInternal().split(" ")) {
list.getInternal().add(new TextTag(arg));
}
args = list;
internal.setArguments(tt.getInternal());
}
else {
super.applyDetermination(errors, determination, value);
}
}
}

0 comments on commit 9969bae

Please sign in to comment.