SteamWar/MissileWars
Archiviert
13
0

Add LocationScript

Dieser Commit ist enthalten in:
jojo 2020-11-01 09:35:35 +01:00
Ursprung b7ef14f814
Commit 090185558f
4 geänderte Dateien mit 120 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -24,11 +24,22 @@ import org.bukkit.entity.Entity;
public interface RunnableScript {
enum LocationType {
STATIC,
DYNAMIC,
DEFAULT,
CUSTOM
}
class RunnableScriptEvent {
public final ScriptedItem.EventType eventType;
public final Entity entity;
private final Location location;
private Location customLocation;
private LocationType locationType = LocationType.DEFAULT;
public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity, Location location) {
this.eventType = eventType;
@ -37,6 +48,23 @@ public interface RunnableScript {
}
public Location getLocation() {
// Custom location
if (locationType == LocationType.CUSTOM && customLocation != null) {
return customLocation;
}
// Static initial Location
if (locationType == LocationType.STATIC) {
return location;
}
// Dynamic Location if entity is not null
if (locationType == LocationType.DYNAMIC) {
if (entity != null) return entity.getLocation();
return location;
}
// Default Location is static if EventType is onClick otherwise dynamic
if (eventType == ScriptedItem.EventType.onClick) return location;
if (entity != null) {
return entity.getLocation();
@ -44,6 +72,19 @@ public interface RunnableScript {
return location;
}
public void setLocationType(LocationType locationType) {
if (locationType == null) return;
this.locationType = locationType;
}
public void setCustomLocation(double x, double y, double z) {
setCustomLocation(x, y, z, 0, 0);
}
public void setCustomLocation(double x, double y, double z, float pitch, float yaw) {
this.customLocation = new Location(location.getWorld(), x, y, z, yaw, pitch);
}
}
boolean execute(RunnableScriptEvent runnableScriptEvent);

Datei anzeigen

@ -49,6 +49,8 @@ public class ScriptParser {
return new RemoveScript(jsonObject);
case "launch":
return new LaunchScript(jsonObject);
case "location":
return new LocationScript(jsonObject);
case "paste":
return new PasteScript(jsonObject);
case "potion":

Datei anzeigen

@ -55,7 +55,7 @@ public class DelayScript implements RunnableScript {
default:
break;
}
} else {
} else if (jsonPrimitive.isNumber()) {
delayTime = jsonPrimitive.getAsInt();
}
}

Datei anzeigen

@ -0,0 +1,76 @@
package de.steamwar.misslewars.scripts.implemented;
import com.google.gson.JsonObject;
import de.steamwar.misslewars.scripts.RunnableScript;
import org.bukkit.Location;
public class LocationScript implements RunnableScript {
private LocationType locationType = null;
private LocationExecutor locationExecutor = null;
private interface LocationExecutor {
void location(RunnableScriptEvent runnableScriptEvent);
}
public LocationScript(JsonObject location) {
if (location.has("location")) {
JsonObject jsonObject = location.getAsJsonObject("location");
double x = jsonObject.getAsJsonPrimitive("x").getAsDouble();
double y = jsonObject.getAsJsonPrimitive("y").getAsDouble();
double z = jsonObject.getAsJsonPrimitive("z").getAsDouble();
switch (jsonObject.getAsJsonPrimitive("type").getAsString().toLowerCase()) {
case "absolute":
case "fix":
case "fixed":
locationExecutor = runnableScriptEvent -> runnableScriptEvent.setCustomLocation(x, y, z);
break;
case "offsetEntity":
locationExecutor = runnableScriptEvent -> {
if (runnableScriptEvent.entity == null) {
return;
}
Location location1 = runnableScriptEvent.entity.getLocation();
runnableScriptEvent.setCustomLocation(location1.getX() + x, location1.getY() + y, location1.getZ() + z);
};
break;
case "offsetLocation":
locationExecutor = runnableScriptEvent -> {
Location location1 = runnableScriptEvent.getLocation();
runnableScriptEvent.setCustomLocation(location1.getX() + x, location1.getY() + y, location1.getZ() + z);
};
break;
}
locationType = LocationType.CUSTOM;
} else if (location.has("locationType")) {
switch (location.getAsJsonPrimitive("locationType").getAsString().toLowerCase()) {
case "static":
locationType = LocationType.STATIC;
break;
case "dynamic":
locationType = LocationType.DYNAMIC;
break;
case "custom":
locationType = LocationType.CUSTOM;
break;
case "default":
default:
locationType = LocationType.DEFAULT;
break;
}
}
}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
if (locationType == null) return false;
runnableScriptEvent.setLocationType(locationType);
if (locationExecutor != null) {
locationExecutor.location(runnableScriptEvent);
}
return true;
}
}