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

Commit

Permalink
Catalog Type Switches Improved
Browse files Browse the repository at this point in the history
Also improved some DurationTag tags.
  • Loading branch information
Xenmai committed Oct 18, 2017
1 parent abb84a3 commit 0239571
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 51 deletions.
Expand Up @@ -10,8 +10,10 @@
import com.denizenscript.denizen2core.utilities.CoreUtilities;
import com.denizenscript.denizen2sponge.tags.objects.*;
import com.denizenscript.denizen2sponge.utilities.UtilLocation;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import com.denizenscript.denizen2sponge.utilities.flags.FlagHelper;
import com.denizenscript.denizen2sponge.utilities.flags.FlagMap;
import org.spongepowered.api.CatalogType;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.data.type.HandType;
Expand Down Expand Up @@ -105,37 +107,28 @@ public static boolean checkWeather(String weather, ScriptEvent.ScriptEventData d
}

public static boolean checkWeather(String weather, ScriptEvent.ScriptEventData data, Action<String> error, String tname) {
if (!data.switches.containsKey(tname)) {
return true;
}
for (AbstractTagObject ato : ListTag.getFor(error, data.switches.get(tname)).getInternal()) {
Optional<Weather> type = Sponge.getRegistry().getType(Weather.class, ato.toString());
if (!type.isPresent()) {
error.run("Invalid weather type: '" + ato.debug() + "'!");
return false;
}
else if (type.get().getId().equals(weather)) {
return true;
}
}
return false;
return checkCatalogType(Weather.class, weather, data, error, tname);
}

public static boolean checkHandType(String hand, ScriptEvent.ScriptEventData data, Action<String> error) {
return checkHandType(hand, data, error, "hand");
}

public static boolean checkHandType(String hand, ScriptEvent.ScriptEventData data, Action<String> error, String tname) {
return checkCatalogType(HandType.class, hand, data, error, tname);
}

public static boolean checkCatalogType(Class clazz, String type, ScriptEvent.ScriptEventData data, Action<String> error, String tname) {
if (!data.switches.containsKey(tname)) {
return true;
}
for (AbstractTagObject ato : ListTag.getFor(error, data.switches.get(tname)).getInternal()) {
Optional<HandType> type = Sponge.getRegistry().getType(HandType.class, ato.toString());
if (!type.isPresent()) {
error.run("Invalid hand type: '" + ato.debug() + "'!");
Optional<CatalogType> opt = Sponge.getRegistry().getType(clazz, ato.toString());
if (!opt.isPresent()) {
error.run("Invalid " + clazz.getSimpleName() + " type: '" + ato.debug() + "'!");
return false;
}
else if (type.get().getId().equals(hand)) {
else if (Utilities.getIdWithoutDefaultPrefix(opt.get().getId()).equals(type)) {
return true;
}
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import com.denizenscript.denizen2sponge.tags.objects.ItemTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.tags.objects.PlayerTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.data.Transaction;
Expand Down Expand Up @@ -66,7 +67,8 @@ public boolean matches(ScriptEventData data) {
.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.empty())), data, this::error)
&& D2SpongeEventHelper.checkWorld(location.getInternal().world, data, this::error)
&& D2SpongeEventHelper.checkCuboid(location.getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWeather(location.getInternal().world.getWeather().getId(), data, this::error);
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
location.getInternal().world.getWeather().getId()), data, this::error);
}

public PlayerTag player;
Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.denizenscript.denizen2sponge.tags.objects.BlockTypeTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.tags.objects.PlayerTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.data.Transaction;
Expand Down Expand Up @@ -61,7 +62,8 @@ public boolean matches(ScriptEvent.ScriptEventData data) {
return D2SpongeEventHelper.checkBlockType(material.getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWorld(location.getInternal().world, data, this::error)
&& D2SpongeEventHelper.checkCuboid(location.getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWeather(location.getInternal().world.getWeather().getId(), data, this::error);
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
location.getInternal().world.getWeather().getId()), data, this::error);
}

public PlayerTag player;
Expand Down
Expand Up @@ -135,7 +135,7 @@ public void onRightClickBlock(InteractBlockEvent.Secondary evt, @Root Player pla
event.impact_normal = new LocationTag(0, 0, 0);
}
event.hInternal = evt.getHandType();
event.hand = new TextTag(evt.getHandType().getId());
event.hand = new TextTag(Utilities.getIdWithoutDefaultPrefix(evt.getHandType().getId()));
event.cancelled = evt.isCancelled();
event.run();
evt.setCancelled(event.cancelled);
Expand Down
Expand Up @@ -9,6 +9,7 @@
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.ItemTag;
import com.denizenscript.denizen2sponge.tags.objects.PlayerTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.type.HandType;
import org.spongepowered.api.entity.living.player.Player;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void onRightClickEntity(InteractEntityEvent.Secondary evt, @Root Player p
event.player = new PlayerTag(player);
event.entity = new EntityTag(evt.getTargetEntity());
event.hInternal = evt.getHandType();
event.hand = new TextTag(evt.getHandType().getId());
event.hand = new TextTag(Utilities.getIdWithoutDefaultPrefix(evt.getHandType().getId()));
event.cancelled = evt.isCancelled();
event.run();
evt.setCancelled(event.cancelled);
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.BlockTypeTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.data.Transaction;
Expand Down Expand Up @@ -59,7 +60,8 @@ public boolean matches(ScriptEventData data) {
&& D2SpongeEventHelper.checkBlockType(old_material.getInternal(), data, this::error, "old_type")
&& D2SpongeEventHelper.checkWorld(location.getInternal().world, data, this::error)
&& D2SpongeEventHelper.checkCuboid(location.getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWeather(location.getInternal().world.getWeather().getId(), data, this::error);
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
location.getInternal().world.getWeather().getId()), data, this::error);

}

Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.BlockTypeTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.data.Transaction;
Expand Down Expand Up @@ -56,7 +57,8 @@ public boolean matches(ScriptEventData data) {
return D2SpongeEventHelper.checkBlockType(material.getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWorld(location.getInternal().world, data, this::error)
&& D2SpongeEventHelper.checkCuboid(location.getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWeather(location.getInternal().world.getWeather().getId(), data, this::error);
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
location.getInternal().world.getWeather().getId()), data, this::error);
}

public LocationTag location;
Expand Down
Expand Up @@ -10,6 +10,7 @@
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.Listener;
Expand Down Expand Up @@ -63,7 +64,8 @@ public boolean couldMatch(ScriptEventData data) {
public boolean matches(ScriptEventData data) {
return D2SpongeEventHelper.checkWorld(location.getInternal().world, data, this::error)
&& D2SpongeEventHelper.checkCuboid(location.getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWeather(location.getInternal().world.getWeather().getId(), data, this::error);
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
location.getInternal().world.getWeather().getId()), data, this::error);
}

public LocationTag location;
Expand Down
Expand Up @@ -5,6 +5,7 @@
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.world.ConstructPortalEvent;
Expand Down Expand Up @@ -52,7 +53,8 @@ public boolean couldMatch(ScriptEventData data) {
public boolean matches(ScriptEventData data) {
return D2SpongeEventHelper.checkWorld(location.getInternal().world, data, this::error)
&& D2SpongeEventHelper.checkCuboid(location.getInternal(), data, this::error)
&& D2SpongeEventHelper.checkWeather(location.getInternal().world.getWeather().getId(), data, this::error);
&& D2SpongeEventHelper.checkWeather(Utilities.getIdWithoutDefaultPrefix(
location.getInternal().world.getWeather().getId()), data, this::error);
}

public LocationTag location;
Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.events.D2SpongeEventHelper;
import com.denizenscript.denizen2sponge.tags.objects.WorldTag;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.world.ChangeWorldWeatherEvent;
Expand Down Expand Up @@ -98,9 +99,9 @@ public void onWeatherChanges(ChangeWorldWeatherEvent evt) {
WeatherChangesScriptEvent event = (WeatherChangesScriptEvent) clone();
event.internal = evt;
event.world = new WorldTag(evt.getTargetWorld());
event.duration = new DurationTag(evt.getDuration() / 20.0);
event.new_weather = new TextTag(evt.getWeather().getId());
event.old_weather = new TextTag(evt.getInitialWeather().getId());
event.duration = new DurationTag(evt.getDuration() * (1.0 / 20.0));
event.new_weather = new TextTag(Utilities.getIdWithoutDefaultPrefix(evt.getWeather().getId()));
event.old_weather = new TextTag(Utilities.getIdWithoutDefaultPrefix(evt.getInitialWeather().getId()));
event.cancelled = evt.isCancelled();
event.run();
evt.setCancelled(event.cancelled);
Expand All @@ -120,7 +121,7 @@ else if (determination.equals("new_weather")) {
this.error("Invalid weather type: '" + tt.debug() + "'!");
return;
}
new_weather = new TextTag(type.get().getId());
new_weather = new TextTag(Utilities.getIdWithoutDefaultPrefix(type.get().getId()));
internal.setWeather(type.get());
}
else {
Expand Down
Expand Up @@ -420,7 +420,7 @@ public String friendlyName() {
// @ReturnType DurationTag
// @Returns the maximum air level this entity can have.
// -->
handlers.put("max_air", (dat, obj) -> new DurationTag(((EntityTag) obj).internal.get(Keys.MAX_AIR).get() / 20.0));
handlers.put("max_air", (dat, obj) -> new DurationTag(((EntityTag) obj).internal.get(Keys.MAX_AIR).get() * (1.0 / 20.0)));
// <--[tag]
// @Name EntityTag.remaining_air
// @Updated 2017/04/17
Expand All @@ -432,9 +432,9 @@ public String friendlyName() {
Entity ent = ((EntityTag) obj).internal;
Optional<Integer> opt = ent.get(Keys.REMAINING_AIR);
if (!opt.isPresent()) {
return new DurationTag(ent.get(Keys.MAX_AIR).get() / 20.0);
return new DurationTag(ent.get(Keys.MAX_AIR).get() * (1.0 / 20.0));
}
return new DurationTag(opt.get() / 20.0);
return new DurationTag(opt.get() * (1.0 / 20.0));
});
// <--[tag]
// @Name EntityTag.inventory
Expand Down Expand Up @@ -496,10 +496,10 @@ public String friendlyName() {
// @Name EntityTag.fall_time
// @Updated 2017/09/28
// @Group Current Information
// @ReturnType IntegerTag
// @ReturnType DurationTag
// @Returns how long the block has been falling. Falling block entities only.
// -->
handlers.put("fall_time", (dat, obj) -> new IntegerTag(((FallingBlock) ((EntityTag) obj).internal).fallTime().get()));
handlers.put("fall_time", (dat, obj) -> new DurationTag(((FallingBlock) ((EntityTag) obj).internal).fallTime().get() * (1.0 / 20.0)));
// <--[tag]
// @Name EntityTag.max_fall_damage
// @Updated 2017/09/28
Expand Down Expand Up @@ -634,10 +634,10 @@ else if (source instanceof Entity) {
// @Name EntityTag.application_delay
// @Updated 2017/10/02
// @Group Current Information
// @ReturnType IntegerTag
// @ReturnType DurationTag
// @Returns the application delay of this cloud. Area effect cloud entities only.
// -->
handlers.put("application_delay", (dat, obj) -> new IntegerTag(((AreaEffectCloud) ((EntityTag) obj).internal).applicationDelay().get()));
handlers.put("application_delay", (dat, obj) -> new DurationTag(((AreaEffectCloud) ((EntityTag) obj).internal).applicationDelay().get() * (1.0 / 20.0)));
// <--[tag]
// @Name EntityTag.cloud_color
// @Updated 2017/10/04
Expand All @@ -653,18 +653,18 @@ else if (source instanceof Entity) {
// @Name EntityTag.duration
// @Updated 2017/10/02
// @Group Current Information
// @ReturnType IntegerTag
// @ReturnType DurationTag
// @Returns the current duration of this cloud. Area effect cloud entities only.
// -->
handlers.put("duration", (dat, obj) -> new IntegerTag(((AreaEffectCloud) ((EntityTag) obj).internal).duration().get()));
handlers.put("duration", (dat, obj) -> new DurationTag(((AreaEffectCloud) ((EntityTag) obj).internal).duration().get() * (1.0 / 20.0)));
// <--[tag]
// @Name EntityTag.duration_on_use
// @Updated 2017/10/02
// @Group Current Information
// @ReturnType IntegerTag
// @ReturnType DurationTag
// @Returns how much duration the cloud will lose after applying its effect to an entity. Area effect cloud entities only.
// -->
handlers.put("duration_on_use", (dat, obj) -> new IntegerTag(((AreaEffectCloud) ((EntityTag) obj).internal).durationOnUse().get()));
handlers.put("duration_on_use", (dat, obj) -> new DurationTag(((AreaEffectCloud) ((EntityTag) obj).internal).durationOnUse().get() * (1.0 / 20.0)));
// <--[tag]
// @Name EntityTag.particle_type
// @Updated 2017/10/02
Expand Down Expand Up @@ -701,10 +701,10 @@ else if (source instanceof Entity) {
// @Name EntityTag.wait_time
// @Updated 2017/10/02
// @Group Current Information
// @ReturnType IntegerTag
// @ReturnType DurationTag
// @Returns how long until this cloud will affect entities with its effect. Area effect cloud entities only.
// -->
handlers.put("wait_time", (dat, obj) -> new IntegerTag(((AreaEffectCloud) ((EntityTag) obj).internal).waitTime().get()));
handlers.put("wait_time", (dat, obj) -> new DurationTag(((AreaEffectCloud) ((EntityTag) obj).internal).waitTime().get() * (1.0 / 20.0)));
// <--[tag]
// @Name EntityTag.age
// @Updated 2017/10/02
Expand Down

0 comments on commit 0239571

Please sign in to comment.