Skip to content

Commit

Permalink
Add piglin bartering event (#6768)
Browse files Browse the repository at this point in the history
* Added on piglin bartering

* Added bartering input

* Added bartering output

* Minor formatting/documentation fixes

* Fixed IDE using wildcard import

* Epically forgot space

* Fixed review comments

* Okay, should be good now

* Added junit tests

* Fix tests for <1.16 versions

* uhh

* Only register exprs if event exists

* Remove plagiarism

* Minor spelling mistake

* Fixed errors

* Fixed no completion

* oopsie

* tried parsing section

* forgot "to console"

* move to file loading

* rename file

* remove broadcast

* Update src/main/java/ch/njol/skript/expressions/ExprBarterDrops.java

Co-authored-by: Patrick Miller <[email protected]>

* Update src/main/java/ch/njol/skript/expressions/ExprBarterDrops.java

Co-authored-by: Patrick Miller <[email protected]>

* Update src/main/java/ch/njol/skript/expressions/ExprBarterInput.java

Co-authored-by: Patrick Miller <[email protected]>

* Update src/test/skript/junit/EvtPiglinBarterTest.sk

Co-authored-by: Patrick Miller <[email protected]>

* Apply suggestions from code review

Didn't realise this existed until now. Oops!

Co-authored-by: Patrick Miller <[email protected]>

* Fixed review stuff

* Extra nls

Co-authored-by: Patrick Miller <[email protected]>

* oops

* fixes

* moar fixes

* thanks pickle

---------

Co-authored-by: Moderocky <[email protected]>
Co-authored-by: Patrick Miller <[email protected]>
Co-authored-by: sovdee <[email protected]>
  • Loading branch information
4 people authored Aug 30, 2024
1 parent 4e584b2 commit de1a529
Show file tree
Hide file tree
Showing 6 changed files with 416 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.entity.SheepRegrowWoolEvent;
import org.bukkit.event.entity.SlimeSplitEvent;
import org.bukkit.event.entity.PiglinBarterEvent;
import org.bukkit.event.inventory.FurnaceBurnEvent;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
Expand Down Expand Up @@ -749,6 +750,20 @@ public class SimpleEvents {
)
.since("2.7");

if (Skript.classExists("org.bukkit.event.entity.PiglinBarterEvent")) {
Skript.registerEvent("Piglin Barter", SimpleEvent.class, PiglinBarterEvent.class, "piglin (barter[ing]|trad(e|ing))")
.requiredPlugins("Minecraft 1.16+")
.description(
"Called when a piglin finishes bartering. A piglin may start bartering after picking up an item on its bartering list.",
"Cancelling will prevent piglins from dropping items, but will still make them pick up the input.")
.examples(
"on piglin barter:",
"\tif barter drops contain diamond:",
"\t\tsend \"Diamonds belong in the money pit!\" to player",
"\t\tcancel event"
)
.since("INSERT VERSION");
}
{
final Class<? extends Event> eventClass;
if (Skript.classExists("org.bukkit.event.block.BellRingEvent")) {
Expand Down Expand Up @@ -787,6 +802,7 @@ public class SimpleEvents {
)
.since("2.9.0")
.requiredPlugins("Spigot 1.19.4+");

}

if (Skript.classExists("com.destroystokyo.paper.event.entity.EndermanAttackPlayerEvent")) {
Expand All @@ -805,5 +821,4 @@ public class SimpleEvents {
.requiredPlugins("Paper");
}
}

}
150 changes: 150 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprBarterDrops.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.bukkit.event.entity.PiglinBarterEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.List;

@Name("Barter Drops")
@Description("The items dropped by the piglin in a piglin bartering event.")
@Examples({
"on piglin barter:",
"\tif the bartering drops contain a jack-o-lantern:",
"\t\tremove jack-o-lantern from bartering output",
"\t\tbroadcast \"it's not halloween yet!\""
})
@Since("INSERT VERSION")
public class ExprBarterDrops extends SimpleExpression<ItemType> {

static {
if (Skript.classExists("org.bukkit.event.entity.PiglinBarterEvent")) {
Skript.registerExpression(ExprBarterDrops.class, ItemType.class,
ExpressionType.SIMPLE, "[the] [piglin] barter[ing] drops");
}
}

private Kleenean delay;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult result) {
if (!getParser().isCurrentEvent(PiglinBarterEvent.class)) {
Skript.error("The expression 'barter drops' can only be used in the piglin bartering event");
return false;
}

delay = isDelayed;

return true;
}

@Override
@Nullable
protected ItemType[] get(Event event) {
if (!(event instanceof PiglinBarterEvent))
return null;

return ((PiglinBarterEvent) event).getOutcome()
.stream()
.map(ItemType::new)
.toArray(ItemType[]::new);
}

@Override
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
if (!delay.isFalse()) {
Skript.error("Can't change the piglin bartering drops after the event has already passed");
return null;
}

switch (mode) {
case SET:
case ADD:
case REMOVE:
case REMOVE_ALL:
case DELETE:
return CollectionUtils.array(ItemType[].class);
default:
return null;
}
}

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
if (!(event instanceof PiglinBarterEvent))
return;

List<ItemStack> outcome = ((PiglinBarterEvent) event).getOutcome();

switch (mode) {
case SET:
outcome.clear();
case ADD:
for (Object item : delta) {
((ItemType) item).addTo(outcome);
}
break;
case REMOVE:
for (Object item : delta) {
((ItemType) item).removeFrom(false, outcome);
}
break;
case REMOVE_ALL:
for (Object item : delta) {
((ItemType) item).removeAll(false, outcome);
}
break;
case DELETE:
outcome.clear();
break;
}
}

@Override
public boolean isSingle() {
return false;
}

@Override
public Class<? extends ItemType> getReturnType() {
return ItemType.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the barter drops";
}

}
86 changes: 86 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprBarterInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.event.entity.PiglinBarterEvent;
import org.jetbrains.annotations.Nullable;

@Name("Barter Input")
@Description("The item picked up by the piglin in a piglin bartering event.")
@Examples({
"on piglin barter:",
"\tif the bartering input is a gold ingot:",
"\t\tbroadcast \"my precious...\""
})
@Since("INSERT VERSION")
public class ExprBarterInput extends SimpleExpression<ItemType> {

static {
if (Skript.classExists("org.bukkit.event.entity.PiglinBarterEvent")) {
Skript.registerExpression(ExprBarterInput.class, ItemType.class,
ExpressionType.SIMPLE, "[the] [piglin] barter[ing] input");
}
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult result) {
if (!getParser().isCurrentEvent(PiglinBarterEvent.class)) {
Skript.error("The expression 'barter input' can only be used in the piglin bartering event");
return false;
}
return true;
}

@Override
@Nullable
protected ItemType[] get(Event event) {
if (!(event instanceof PiglinBarterEvent))
return null;

return new ItemType[] { new ItemType(((PiglinBarterEvent) event).getInput()) };
}

@Override
public boolean isSingle() {
return true;
}

@Override
public Class<? extends ItemType> getReturnType() {
return ItemType.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the barter input";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.test.tests.syntaxes.events;

import ch.njol.skript.Skript;
import ch.njol.skript.test.runner.SkriptJUnitTest;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class EvtPiglinBarterTest extends SkriptJUnitTest {

private Entity piglin;
private static final boolean canRun = Skript.classExists("org.bukkit.event.entity.PiglinBarterEvent");

static {
setShutdownDelay(1);
}

@Before
public void spawn() {
if (!canRun)
return;

piglin = getTestWorld().spawnEntity(getTestLocation(), EntityType.PIGLIN);
}

@Test
public void testCall() {
if (!canRun)
return;

ItemStack input = new ItemStack(Material.GOLD_INGOT);
List<ItemStack> outcome = new ArrayList<>();
outcome.add(new ItemStack(Material.EMERALD));

try {
Bukkit.getPluginManager().callEvent(
new org.bukkit.event.entity.PiglinBarterEvent(
(org.bukkit.entity.Piglin) piglin, input, outcome));
} catch (NoClassDefFoundError ignored) { }
}

@After
public void remove() {
if (!canRun)
return;

piglin.remove();
}

}
Loading

0 comments on commit de1a529

Please sign in to comment.