-
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
4e584b2
commit de1a529
Showing
6 changed files
with
416 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/main/java/ch/njol/skript/expressions/ExprBarterDrops.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
src/main/java/ch/njol/skript/expressions/ExprBarterInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
src/test/java/org/skriptlang/skript/test/tests/syntaxes/events/EvtPiglinBarterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
Oops, something went wrong.