Skip to content

Commit 6f7fb56

Browse files
Jakubk15gemini-code-assist[bot]P1otrullaRollcziCitralFlo
authored
GH-1134 Add permissions allowing to ignore sleep count for night skip and phantom spawn (#1135)
* Add sleep controllers for ignore sleeping and preventing spawn of phantoms when holding appropriate permission * Add a new line between static field and class declaration * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/sleep/PlayerJoinSleepController.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * improve event fields and add javadoc * Update PhantomSpawnAttemptEvent.java Co-authored-by: Piotr Zych <[email protected]> * Update PhantomSpawnAttemptEvent.java Co-authored-by: Piotr Zych <[email protected]> * Update PhantomSpawnAttemptEvent.java Co-authored-by: Piotr Zych <[email protected]> * Update PhantomSpawnAttemptEvent.java Co-authored-by: Piotr Zych <[email protected]> * Update PhantomSpawnAttemptEvent.java Co-authored-by: Piotr Zych <[email protected]> * Update PlayerJoinSleepController.java Co-authored-by: Piotr Zych <[email protected]> * Update PhantomSpawnAttemptEvent.java Co-authored-by: Piotr Zych <[email protected]> * Adjust event field names to P1otrulla's demands * remove duplicate code to fix build * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/sleep/PhantomSpawnController.java Co-authored-by: Norbert Dejlich <[email protected]> * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/sleep/PlayerJoinSleepController.java Co-authored-by: Norbert Dejlich <[email protected]> * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/sleep/PhantomSpawnController.java Co-authored-by: Norbert Dejlich <[email protected]> * inline field * Update PhantomSpawnController.java Co-authored-by: Michał Wojtas <[email protected]> * Update PlayerJoinSleepController.java Co-authored-by: Michał Wojtas <[email protected]> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Piotr Zych <[email protected]> Co-authored-by: Norbert Dejlich <[email protected]> Co-authored-by: Michał Wojtas <[email protected]>
1 parent f39f11d commit 6f7fb56

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.eternalcode.core.feature.sleep;
2+
3+
import com.eternalcode.annotations.scan.permission.PermissionDocs;
4+
import com.eternalcode.core.injector.annotations.Inject;
5+
import com.eternalcode.core.injector.annotations.component.Controller;
6+
import com.eternalcode.paper.phantom.PhantomEventInitializer;
7+
import com.eternalcode.paper.phantom.PhantomSpawnAttemptEvent;
8+
import org.bukkit.entity.Player;
9+
import org.bukkit.event.EventHandler;
10+
import org.bukkit.event.Listener;
11+
import org.bukkit.plugin.Plugin;
12+
13+
@Controller
14+
@PermissionDocs(
15+
name = "Phantom Spawn Ignore",
16+
permission = "eternalcore.sleep.noinsomnia",
17+
description = "Player with this permission will not spawn phantoms"
18+
)
19+
class PhantomSpawnController implements Listener {
20+
21+
@Inject
22+
PhantomSpawnController(Plugin plugin) {
23+
new PhantomEventInitializer(plugin).initialize();
24+
}
25+
26+
@EventHandler
27+
void onPhantomSpawnAttempt(PhantomSpawnAttemptEvent event) {
28+
if (event.getEntity() instanceof Player player) {
29+
if (player.hasPermission("eternalcore.sleep.noinsomnia")) {
30+
event.setCancelled(true);
31+
}
32+
}
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.eternalcode.core.feature.sleep;
2+
3+
import com.eternalcode.annotations.scan.permission.PermissionDocs;
4+
import com.eternalcode.core.injector.annotations.component.Controller;
5+
import org.bukkit.entity.Player;
6+
import org.bukkit.event.EventHandler;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.player.PlayerJoinEvent;
9+
10+
@Controller
11+
@PermissionDocs(
12+
name = "Sleep Ignore",
13+
permission = "eternalcore.sleep.ignore",
14+
description = "Player with this permission will ignore sleep count needed to skip the night"
15+
)
16+
class PlayerJoinSleepController implements Listener {
17+
18+
private static final String SLEEP_IGNORE_PERMISSION = "eternalcore.sleep.ignore";
19+
20+
@EventHandler
21+
void onPlayerJoin(PlayerJoinEvent event) {
22+
Player player = event.getPlayer();
23+
if (player.hasPermission(SLEEP_IGNORE_PERMISSION)) {
24+
player.setSleepingIgnored(true);
25+
}
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.eternalcode.paper.phantom;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.plugin.Plugin;
5+
6+
public class PhantomEventInitializer {
7+
8+
private final Plugin plugin;
9+
10+
public PhantomEventInitializer(Plugin plugin) {
11+
this.plugin = plugin;
12+
}
13+
14+
public void initialize() {
15+
Bukkit.getPluginManager().registerEvents(new PhantomEventWrapper(), this.plugin);
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.eternalcode.paper.phantom;
2+
3+
import com.destroystokyo.paper.event.entity.PhantomPreSpawnEvent;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.event.EventHandler;
6+
import org.bukkit.event.EventPriority;
7+
import org.bukkit.event.Listener;
8+
9+
class PhantomEventWrapper implements Listener {
10+
11+
@EventHandler(priority = EventPriority.HIGH)
12+
void onPhantomSpawn(PhantomPreSpawnEvent event) {
13+
PhantomSpawnAttemptEvent attemptEvent = new PhantomSpawnAttemptEvent(
14+
event.getSpawningEntity(),
15+
event.getSpawnLocation(),
16+
event.getReason()
17+
);
18+
19+
Bukkit.getPluginManager().callEvent(attemptEvent);
20+
21+
if (attemptEvent.isCancelled()) {
22+
event.setCancelled(true);
23+
}
24+
}
25+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.eternalcode.paper.phantom;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.entity.Entity;
5+
import org.bukkit.event.Cancellable;
6+
import org.bukkit.event.Event;
7+
import org.bukkit.event.HandlerList;
8+
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
/**
12+
* Internal event of {@link com.destroystokyo.paper.event.entity.PhantomPreSpawnEvent}
13+
*/
14+
public class PhantomSpawnAttemptEvent extends Event implements Cancellable {
15+
16+
private static final HandlerList HANDLER_LIST = new HandlerList();
17+
18+
private final Entity entity;
19+
private final Location location;
20+
private final SpawnReason spawnReason;
21+
private boolean cancelled = false;
22+
23+
public PhantomSpawnAttemptEvent(Entity entity, Location location, SpawnReason spawnReason) {
24+
this.entity = entity;
25+
this.location = location;
26+
this.spawnReason = spawnReason;
27+
}
28+
29+
public Entity getEntity() {
30+
return this.entity;
31+
}
32+
33+
public Location getLocation() {
34+
return this.location;
35+
}
36+
37+
public SpawnReason getSpawnReason() {
38+
return this.spawnReason;
39+
}
40+
41+
@Override
42+
public boolean isCancelled() {
43+
return cancelled;
44+
}
45+
46+
@Override
47+
public void setCancelled(boolean cancelled) {
48+
this.cancelled = cancelled;
49+
}
50+
51+
@Override
52+
public @NotNull HandlerList getHandlers() {
53+
return HANDLER_LIST;
54+
}
55+
56+
public static HandlerList getHandlerList() {
57+
return HANDLER_LIST;
58+
}
59+
}

0 commit comments

Comments
 (0)