Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlayerExpCooldown #7076

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
27ba1b6
Starter Commit
TheAbsolutionism Sep 13, 2024
6ad8189
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Sep 13, 2024
b3bcd25
Changes + Tests
TheAbsolutionism Sep 13, 2024
14f396c
Requested Changes
TheAbsolutionism Sep 14, 2024
bdb2c82
Requested Changes
TheAbsolutionism Sep 15, 2024
fd34dc4
Requested Changes
TheAbsolutionism Sep 16, 2024
8eb4004
Requested Change
TheAbsolutionism Sep 18, 2024
cfe20fd
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Sep 19, 2024
32bf555
Requested Changes
TheAbsolutionism Sep 20, 2024
df2b647
Requested Changes
TheAbsolutionism Sep 21, 2024
98f3121
Fix Syntax
TheAbsolutionism Sep 21, 2024
1f5908e
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Sep 21, 2024
f793978
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Sep 22, 2024
f0ac978
Conflict
TheAbsolutionism Sep 22, 2024
bda5f80
Requested Changes
TheAbsolutionism Sep 22, 2024
0ac4b26
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Sep 22, 2024
8562a17
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Oct 1, 2024
877f948
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Oct 2, 2024
45c3604
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Oct 6, 2024
612796a
Merge remote-tracking branch 'upstream/dev/feature' into dev/PlayerEx…
TheAbsolutionism Oct 13, 2024
1b1e46a
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Oct 13, 2024
77e2703
Final Fix
TheAbsolutionism Oct 13, 2024
e5d04ce
Merge branch 'dev/PlayerExpCooldown' of https://github.com/TheAbsolut…
TheAbsolutionism Oct 13, 2024
e355dbb
Merge branch 'dev/feature' into dev/PlayerExpCooldown
TheAbsolutionism Oct 15, 2024
d84b411
Remove Specific Player Event Value
TheAbsolutionism Oct 16, 2024
bcf3671
Merge branch 'dev/feature' into dev/PlayerExpCooldown
sovdeeth Oct 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.bukkit.event.player.PlayerQuitEvent.QuitReason;
import org.bukkit.event.player.PlayerResourcePackStatusEvent.Status;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent.ChangeReason;
import org.bukkit.inventory.BlockInventoryHolder;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
Expand Down Expand Up @@ -1526,6 +1527,12 @@ public String toVariableNameString(EnchantmentOffer eo) {
.name("Transform Reason")
.description("Represents a transform reason of an <a href='events.html#entity transform'>entity transform event</a>.")
.since("2.8.0"));

Classes.registerClass(new EnumClassInfo<>(ChangeReason.class, "experiencecooldownchangereason", "experience cooldown change reasons")
.user("(experience|[e]xp) ?change (reason|cause)s?")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
.name("Experience Cooldown Change Reason")
.description("Represents a change reason of an <a href='events.html#experience cooldown change event'>experience cooldown change event</a>.")
.since("INSERT VERSION"));
}

}
33 changes: 33 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package ch.njol.skript.classes.data;

import java.time.Duration;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -75,6 +78,7 @@
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockDamageEvent;
Expand Down Expand Up @@ -152,6 +156,8 @@
import org.bukkit.event.player.PlayerPickupArrowEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent.ChangeReason;
import org.bukkit.event.player.PlayerQuitEvent.QuitReason;
import org.bukkit.event.player.PlayerRiptideEvent;
import org.bukkit.event.player.PlayerShearEntityEvent;
Expand Down Expand Up @@ -1935,5 +1941,32 @@ public RegainReason get(EntityRegainHealthEvent event) {
return event.getRegainReason();
}
}, EventValues.TIME_NOW);

// PlayerExpCooldownChangeEvent
EventValues.registerEventValue(PlayerExpCooldownChangeEvent.class, Player.class, new Getter<Player, PlayerExpCooldownChangeEvent>() {
@Override
public @Nullable Player get(PlayerExpCooldownChangeEvent event) {
return event.getPlayer();
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}, EventValues.TIME_NOW);
EventValues.registerEventValue(PlayerExpCooldownChangeEvent.class, ChangeReason.class, new Getter<ChangeReason, PlayerExpCooldownChangeEvent>() {
@Override
public @Nullable ChangeReason get(PlayerExpCooldownChangeEvent event) {
return event.getReason();
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(PlayerExpCooldownChangeEvent.class, Timespan.class, new Getter<Timespan, PlayerExpCooldownChangeEvent>() {
@Override
public @Nullable Timespan get(PlayerExpCooldownChangeEvent event) {
return new Timespan(Timespan.TimePeriod.TICK, event.getNewCooldown());
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(PlayerExpCooldownChangeEvent.class, Timespan.class, new Getter<Timespan, PlayerExpCooldownChangeEvent>() {
@Override
public @Nullable Timespan get(PlayerExpCooldownChangeEvent event) {
return new Timespan(Timespan.TimePeriod.TICK, event.getPlayer().getExpCooldown());
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}, EventValues.TIME_PAST);

}
}
43 changes: 16 additions & 27 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,7 @@
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerAnimationEvent;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerItemBreakEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerItemMendEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLocaleChangeEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerPickupArrowEvent;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerRiptideEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.event.player.PlayerToggleSprintEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.event.vehicle.VehicleCreateEvent;
import org.bukkit.event.vehicle.VehicleDamageEvent;
Expand Down Expand Up @@ -820,5 +794,20 @@ public class SimpleEvents {
.since("2.9.0")
.requiredPlugins("Paper");
}

Skript.registerEvent("Experience Cooldown Change", SimpleEvent.class, PlayerExpCooldownChangeEvent.class, "player (experience|[e]xp) cooldown change")
.description(
"Called when a player's experience cooldown changes.",
"Experience cooldown is how long until a player can pick up another orb of experience."
)
.examples(
"on player experience cooldown change:",
"\tbroadcast event-player",
"\tbroadcast event-timespan",
"\tbroadcast past event-timespan",
"\tbroadcast xp change reason"
)
.since("INSERT VERSION");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package ch.njol.skript.expressions;

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.expressions.base.SimplePropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.Timespan;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Experience Cooldown")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Description({
"The experience cooldown of a player.",
"Experience cooldown is how long until a player can pick up another orb of experience."
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
})
@Examples({
"send experience cooldown of player",
"set experience cooldown of player to 1 hour",
"if experience cooldown of player >= 10 minutes:",
"\tclear experience cooldown of player"
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("INSERT VERSION")
public class ExprExperienceCooldown extends SimplePropertyExpression<Player, Timespan> {

static {
register(ExprExperienceCooldown.class, Timespan.class, "[the] (experience|[e]xp) cooldown", "players");
}

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr((Expression<Player>) exprs[0]);
return true;
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved


@Override
public @Nullable Timespan convert(Player player) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
return new Timespan(Timespan.TimePeriod.TICK, player.getExpCooldown());
}

@Override
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
switch (mode) {
case REMOVE_ALL, DELETE, REMOVE -> {return null;}
}
return CollectionUtils.array(Timespan.class);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
int providedTime = 0;
if (delta[0] != null)
providedTime = (int) ((Timespan) delta[0]).get(Timespan.TimePeriod.TICK);

switch (mode) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
case ADD -> {
for (Player player : getExpr().getArray(event)) {
player.setExpCooldown(player.getExpCooldown() + providedTime);
}
}
case REMOVE -> {
for (Player player : getExpr().getArray(event)) {
player.setExpCooldown(Math.max(player.getExpCooldown() - providedTime, 0));
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
}
case SET -> {
for (Player player : getExpr().getArray(event)) {
player.setExpCooldown(providedTime);
}
}
case RESET -> {
for (Player player : getExpr().getArray(event)) {
player.setExpCooldown(0);
}
}
default -> {}
}
}

@Override
protected String getPropertyName() {
return "experience cooldown";
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ch.njol.skript.expressions;

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.expressions.base.EventValueExpression;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent.ChangeReason;
import org.jetbrains.annotations.Nullable;

@Name("Experience Cooldown Change Reason")
@Description({
"The <a href='classes.html#experiencechangereason'>experience change reason</a> within in an" +
"<a href='events.html#experience cooldown change event'>experience cooldown change event</a>."
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
})
@Examples({
"on player experience cooldown change:",
"\tif xp change reason is plugin:",
"\t\t#Changed by a plugin",
"\telse if xp change reason is orb pickup:",
"\t\t#Changed by picking up xp orb"
})
@Since("INSERT VERSION")
public class ExprExperienceCooldownChangeReason extends EventValueExpression<ChangeReason> {

static {
register(ExprExperienceCooldownChangeReason.class, ChangeReason.class, "[the] (experience|[e]xp) change (reason|cause|type)");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

public ExprExperienceCooldownChangeReason() {
super(ChangeReason.class);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "experience change reason";
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

}
6 changes: 6 additions & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2275,6 +2275,11 @@ transform reasons:
unknown: unknown
infection: infection, villager infection

# -- Change Reasons --
experience cooldown change reasons:
plugin: plugin
pickup_orb: orb pickup, pickup orb

# -- Boolean --
boolean:
true:
Expand Down Expand Up @@ -2354,6 +2359,7 @@ types:
quitreason: quit reason¦s @a
inventoryclosereason: inventory close reason¦s @an
transformreason: transform reason¦s @a
experiencecooldownchangereason: experience cooldown change reason¦s @a

# Skript
weathertype: weather type¦s @a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.skriptlang.skript.test.tests.syntaxes.events;

import ch.njol.skript.test.runner.SkriptJUnitTest;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent.ChangeReason;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

public class EvtExperienceCooldownChangeTest extends SkriptJUnitTest {

private Player easyMockPlayer;

@Before
public void setUp() {
easyMockPlayer = EasyMock.niceMock(Player.class);
}

@Test
public void testEvent() {
PlayerExpCooldownChangeEvent event = new PlayerExpCooldownChangeEvent(easyMockPlayer, 200, ChangeReason.PLUGIN);
Bukkit.getPluginManager().callEvent(event);
}

}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 16 additions & 0 deletions src/test/skript/junit/EvtExperienceCooldownChangeTest.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
on load:
set {_test::1} to "got player"
set {_test::2} to "change reason is plugin"
set {_test::3} to "got new cooldown"

ensure junit test "org.skriptlang.skript.test.tests.syntaxes.events.EvtExperienceCooldownChangeTest" completes {_test::*}

on player experience cooldown change:
set {_test} to "org.skriptlang.skript.test.tests.syntaxes.events.EvtExperienceCooldownChangeTest"
junit test is {_test}
if event-player is set:
complete objective "got player" for junit test {_test}
if xp change reason is plugin:
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
complete objective "change reason is plugin" for junit test {_test}
if event-timespan is set:
complete objective "got new cooldown" for junit test {_test}