Skip to content

Commit

Permalink
A few meta changes and additions (#2244)
Browse files Browse the repository at this point in the history
* Add Item in Off Hand for `- take`

* Add world context to <player.groups>

* Minor Meta fixes

* Update PlayerTag.java

Oopsies forgot to update this.

* Minor Meta updates & JavaDoc

* Removed redundant spaces

* Remove more redundant spaces.

* Correct identify() for TradeTag

* Added Villager Events

Added:
`on villager acquires trade`
`on villager replenishes trade`
`on villager changes profession`
Referencing #1913

* Correct typo on event name.

* Revert "Minor Meta updates & JavaDoc"

This reverts commit d8cfb62.

* Revert "Correct identify() for TradeTag"

This reverts commit dbf6966

* Updates as per request on PR

* Revert "Add Item in Off Hand for `- take`"

This reverts commit 740c34f

* Uncomplicate the PlayerTag.groups tag

* Fix my stupid

* Lil Fix

* This should fix that

* fix the idiocy again, cause idiot.
  • Loading branch information
Fortifier42 committed Aug 12, 2020
1 parent 6f846c9 commit 8743c78
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public static void registerMainEvents() {
ScriptEvent.registerScriptEvent(new SheepDyedScriptEvent());
ScriptEvent.registerScriptEvent(new SheepRegrowsScriptEvent());
ScriptEvent.registerScriptEvent(new SlimeSplitsScriptEvent());
ScriptEvent.registerScriptEvent(new VillagerAcquiresTradeScriptEvent());
ScriptEvent.registerScriptEvent(new VillagerChangesProfessionScriptEvent());
ScriptEvent.registerScriptEvent(new VillagerReplenishesTradeScriptEvent());

// NPC events
if (Depends.citizens != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,18 @@ public boolean couldMatch(ScriptPath path) {
@Override
public boolean matches(ScriptPath path) {
String target = path.eventArgLowerAt(0);

if (!tryEntity(entity, target)) {
return false;
}

if (!runInCheck(path, entity.getLocation())) {
return false;
}

if (path.switches.containsKey("by") && (damager == null || !tryEntity(damager, path.switches.get("by")))) {
return false;
}

if (!runGenericSwitchCheck(path, "cause", cause == null ? null : cause.asString())) {
return false;
}

return super.matches(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,15 @@ public boolean couldMatch(ScriptPath path) {

@Override
public boolean matches(ScriptPath path) {

if (!tryEntity(entity, path.eventArgLowerAt(0))) {
return false;
}

if (!tryMaterial(material, path.eventArgLowerAt(3))) {
return false;
}

if (!runInCheck(path, location)) {
return false;
}

return super.matches(path);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.denizenscript.denizen.events.entity;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.TradeTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.VillagerAcquireTradeEvent;

public class VillagerAcquiresTradeScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// villager acquires trade
//
// @Regex ^on villager acquires trade$
//
// @Group Entity
//
// @Switch in:<area> to only process the event if it occurred within a specified area.
//
// @Cancellable true
//
// @Triggers when a villager acquires a new trade.
//
// @Context
// <context.entity> returns the EntityTag of the villager.
// <context.trade> returns the TradeTag for the new trade.
//
// @Determine
// TradeTag to change the new trade.
// -->

public VillagerAcquiresTradeScriptEvent() {
instance = this;
}

public static VillagerAcquiresTradeScriptEvent instance;
public EntityTag entity;
public VillagerAcquireTradeEvent event;

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("villager acquires trade");
}

@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, entity.getLocation())) {
return false;
}
return super.matches(path);
}

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


@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (TradeTag.matches(determinationObj.toString())) {
event.setRecipe(determinationObj.asType(TradeTag.class, getTagContext(path)).getRecipe());
return true;
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ObjectTag getContext(String name) {
if (name.equals("entity")) {
return entity;
}
else if (name.equals("trade")) {
return new TradeTag(event.getRecipe());
}
return super.getContext(name);
}

@EventHandler
public void onVillagerAcquiresTrade(VillagerAcquireTradeEvent event) {
this.event = event;
fire(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.denizenscript.denizen.events.entity;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.VillagerCareerChangeEvent;

public class VillagerChangesProfessionScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// villager changes profession
//
// @Regex ^on villager changes profession$
//
// @Group Entity
//
// @Switch in:<area> to only process the event if it occurred within a specified area.
//
// @Cancellable true
//
// @Triggers when a villager changes profession.
//
// @Context
// <context.entity> returns the EntityTag of the villager.
// <context.profession> returns the name of the new profession. <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Villager.Profession.html>
// <context.reason> returns the reason for the change. <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/VillagerCareerChangeEvent.ChangeReason.html>
//
// @Determine
// ElementTag to change the profession.
// -->

public VillagerChangesProfessionScriptEvent() {
instance = this;
}

public static VillagerChangesProfessionScriptEvent instance;
public EntityTag entity;
public VillagerCareerChangeEvent event;

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("villager changes profession");
}

@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, entity.getLocation())) {
return false;
}
return super.matches(path);
}

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


@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
try {
Villager.Profession newProfession = Villager.Profession.valueOf(determination.toUpperCase());
event.setProfession(newProfession);
return true;
}
catch (IllegalArgumentException e) {
Debug.echoError("Invalid profession specified: " + determination);
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ObjectTag getContext(String name) {
if (name.equals("entity")) {
return entity;
}
else if (name.equals("reason")) {
return new ElementTag(event.getReason().toString());
}
else if (name.equals("profession")) {
return new ElementTag(event.getProfession().toString());
}
return super.getContext(name);
}

@EventHandler
public void onVillagerChangesProfession(VillagerCareerChangeEvent event) {
this.event = event;
this.entity = new EntityTag(event.getEntity());
fire(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.denizenscript.denizen.events.entity;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.TradeTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.VillagerReplenishTradeEvent;

public class VillagerReplenishesTradeScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// villager replenishes trade
//
// @Regex ^on villager replenishes trade$
//
// @Group Entity
//
// @Switch in:<area> to only process the event if it occurred within a specified area.
//
// @Cancellable true
//
// @Triggers when a villager replenishes a trade.
//
// @Context
// <context.entity> returns the EntityTag of the villager.
// <context.trade> returns the TradeTag for the trade being replenished.
// <context.bonus> returns the number of bonus uses added.
//
// @Determine
// TradeTag to change the trade being replenished.
// ElementTag(Number) to change the number of bonus uses added.
// -->

public VillagerReplenishesTradeScriptEvent() {
instance = this;
}

public static VillagerReplenishesTradeScriptEvent instance;
public EntityTag entity;
public VillagerReplenishTradeEvent event;

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("villager replenishes trade");
}

@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, entity.getLocation())) {
return false;
}
return super.matches(path);
}

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


@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (TradeTag.matches(determinationObj.toString())) {
event.setRecipe(determinationObj.asType(TradeTag.class, getTagContext(path)).getRecipe());
return true;
}
else if (determinationObj instanceof ElementTag && ((ElementTag) determinationObj).isInt()) {
event.setBonus(((ElementTag) determinationObj).asInt());
return true;
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ObjectTag getContext(String name) {
if (name.equals("entity")) {
return entity;
}
else if (name.equals("trade")) {
return new TradeTag(event.getRecipe());
}
else if (name.equals("bonus")) {
return new ElementTag(event.getBonus());
}
return super.getContext(name);
}

@EventHandler
public void onVillagerReplenishesTrade(VillagerReplenishTradeEvent event) {
this.event = event;
fire(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ public String toString() {
public static void registerTags() {

// <--[tag]
// @attribute <CuboidTag.blocks[<material>|...]>
// @attribute <CuboidTag.blocks[(<material>|...)]>
// @returns ListTag(LocationTag)
// @description
// Returns each block location within the CuboidTag.
Expand All @@ -792,7 +792,7 @@ public static void registerTags() {
});

// <--[tag]
// @attribute <CuboidTag.spawnable_blocks[<Material>|...]>
// @attribute <CuboidTag.spawnable_blocks[(<material>|...)]>
// @returns ListTag(LocationTag)
// @description
// Returns each LocationTag within the CuboidTag that is safe for players or similar entities to spawn in.
Expand Down
Loading

0 comments on commit 8743c78

Please sign in to comment.