Skip to content

Commit

Permalink
GH-891 Fix removing catboy using butcher, remove catboy on server shu…
Browse files Browse the repository at this point in the history
…tdown, add catboy config. (#891)

* Fix removing catboy using butcher, remove catboy on server shutdown, add catboy config. Add catboy persistance.

* GH-579 Add repository for language. Remove user settings. (#890)

* Add repository for language. Remove user settings.

* Simplify API

* Provide catboy speed from config.

* Create CatBoyEntityService

---------

Co-authored-by: Norbert Dejlich <[email protected]>
  • Loading branch information
vLuckyyy and Rollczi authored Jan 18, 2025
1 parent 8936fa3 commit 1198dfc
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.eternalcode.core.feature.butcher;

import org.bukkit.entity.Entity;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

public class ButcherEntityRemoveEvent extends Event implements Cancellable {

private static final HandlerList HANDLER_LIST = new HandlerList();

private boolean cancelled;
private final Entity entity;

public ButcherEntityRemoveEvent(Entity entity) {
this.entity = entity;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}

public Entity getEntity() {
return entity;
}

@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.eternalcode.core.database.DatabaseType;
import com.eternalcode.core.feature.afk.AfkSettings;
import com.eternalcode.core.feature.automessage.AutoMessageSettings;
import com.eternalcode.core.feature.catboy.CatBoySettings;
import com.eternalcode.core.feature.chat.ChatSettings;
import com.eternalcode.core.feature.helpop.HelpOpSettings;
import com.eternalcode.core.feature.jail.JailSettings;
Expand Down Expand Up @@ -421,6 +422,24 @@ public Set<String> allowedCommands() {
}
}

@Bean
@Description({ " ", "# 4fun Section" })
FunSection fun = new FunSection();

@Contextual
public static class FunSection implements CatBoySettings {
@Description({
"# Speed of player walk speed while using /catboy feature",
"# Default minecraft walk speed is 0.2"
})
public float catboyWalkSpeed = 0.4F;

@Override
public float getCatboyWalkSpeed() {
return this.catboyWalkSpeed;
}
}

@Override
public Resource resource(File folder) {
return Source.of(folder, "config.yml");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.eternalcode.core.feature.essentials.mob;
package com.eternalcode.core.feature.butcher;

import com.eternalcode.core.bridge.litecommand.argument.AbstractViewerArgument;
import com.eternalcode.core.configuration.implementation.PluginConfiguration;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.viewer.ViewerService;
import com.eternalcode.multification.notice.NoticeBroadcast;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.Viewer;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.eternalcode.core.feature.essentials.mob;
package com.eternalcode.core.feature.butcher;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.core.configuration.implementation.PluginConfiguration;
import com.eternalcode.core.event.EventCaller;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import dev.rollczi.litecommands.annotations.argument.Arg;
Expand All @@ -25,10 +25,12 @@
class ButcherCommand {

private final NoticeService noticeService;
private final EventCaller eventCaller;

@Inject
ButcherCommand(NoticeService noticeService, PluginConfiguration pluginConfiguration) {
ButcherCommand(NoticeService noticeService, EventCaller eventCaller) {
this.noticeService = noticeService;
this.eventCaller = eventCaller;
}

@Execute
Expand All @@ -51,16 +53,21 @@ void execute(@Context Player player, @Arg(ButcherArgument.KEY) int chunks, @Arg(

private void killMobs(Player player, int chunksNumber, MobFilter mobFilter) {
Collection<Chunk> chunks = this.getChunksNearPlayer(player, chunksNumber);

int killedMobs = 0;

for (Chunk chunk : chunks) {
for (Entity entity : chunk.getEntities()) {

if (!mobFilter.filterMob(entity)) {
continue;
}

ButcherEntityRemoveEvent event = new ButcherEntityRemoveEvent(entity);
this.eventCaller.callEvent(event);

if (event.isCancelled()) {
continue;
}

entity.remove();
killedMobs++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.core.feature.essentials.mob;
package com.eternalcode.core.feature.butcher;

import com.eternalcode.core.util.EntityUtil;
import org.bukkit.entity.Animals;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.core.feature.essentials.mob;
package com.eternalcode.core.feature.butcher;

import com.eternalcode.core.bridge.litecommand.argument.AbstractViewerArgument;
import com.eternalcode.core.injector.annotations.Inject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.core.feature.essentials.mob;
package com.eternalcode.core.feature.butcher;

import org.bukkit.entity.Entity;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.core.feature.essentials.mob;
package com.eternalcode.core.feature.butcher;

enum MobType {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.eternalcode.core.feature.catboy;

import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Cat;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.Plugin;

@Service
class CatBoyEntityService {

private final NamespacedKey catboyNamespacedKey;

@Inject
CatBoyEntityService(Plugin plugin) {
this.catboyNamespacedKey = new NamespacedKey(plugin, "catboy");
}

Cat createCatboyEntity(Player player, Cat.Type type) {
Cat cat = (Cat) player.getWorld().spawnEntity(player.getLocation(), EntityType.CAT);
cat.setInvulnerable(true);
cat.setOwner(player);
cat.setAI(false);
cat.setCatType(type);

PersistentDataContainer persistentDataContainer = cat.getPersistentDataContainer();
persistentDataContainer.set(catboyNamespacedKey, PersistentDataType.BOOLEAN, true);

return cat;
}

boolean isCatboy(Cat cat) {
return cat.getPersistentDataContainer().has(catboyNamespacedKey, PersistentDataType.BOOLEAN);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.eternalcode.core.feature.catboy;

public interface CatBoySettings {

float getCatboyWalkSpeed();

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.eternalcode.core.feature.catboy;

import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.feature.butcher.ButcherEntityRemoveEvent;
import com.eternalcode.core.feature.teleport.event.EternalTeleportEvent;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Controller;
import com.eternalcode.core.publish.Subscribe;
import com.eternalcode.core.publish.event.EternalShutdownEvent;
import java.time.Duration;
import java.util.Optional;
import org.bukkit.Server;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.entity.Cat;
Expand All @@ -15,21 +21,27 @@
import org.bukkit.event.entity.EntityDismountEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.time.Duration;
import java.util.Optional;

@Controller
class CatboyController implements Listener {

private static final Duration TICK = Duration.ofMillis(50L);

private final CatboyService catboyService;
private final CatBoyEntityService catBoyEntityService;
private final Scheduler scheduler;
private final Server server;

@Inject
CatboyController(CatboyService catboyService, Scheduler scheduler) {
CatboyController(
CatboyService catboyService,
CatBoyEntityService catBoyEntityService,
Scheduler scheduler,
Server server
) {
this.catboyService = catboyService;
this.catBoyEntityService = catBoyEntityService;
this.scheduler = scheduler;
this.server = server;
}

@EventHandler
Expand Down Expand Up @@ -60,7 +72,9 @@ void onTeleport(EternalTeleportEvent event) {
Catboy catboy = optionalCatboy.get();

this.catboyService.unmarkAsCatboy(event.getPlayer());
this.scheduler.runLater(() -> this.catboyService.markAsCatboy(event.getPlayer(), catboy.selectedType()), TICK);
this.scheduler.runLater(
() -> this.catboyService.markAsCatboy(event.getPlayer(), catboy.selectedType()),
TICK);
}
}

Expand All @@ -85,4 +99,25 @@ void onHit(EntityDamageByEntityEvent event) {
}
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
void onEntityRemoveFromWorld(ButcherEntityRemoveEvent event) {
if (!(event.getEntity() instanceof Cat cat)) {
return;
}

if (this.catBoyEntityService.isCatboy(cat)) {
event.setCancelled(true);
}
}

@Subscribe
void onServerShutdown(EternalShutdownEvent event) {
for (Player onlinePlayer : this.server.getOnlinePlayers()) {
boolean catboy = this.catboyService.isCatboy(onlinePlayer.getUniqueId());

if (catboy) {
this.catboyService.unmarkAsCatboy(onlinePlayer);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,44 @@
import com.eternalcode.core.feature.catboy.event.CatboySwitchEvent;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import org.bukkit.entity.Cat;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.entity.Cat;
import org.bukkit.entity.Player;

@Service
class CatboyServiceImpl implements CatboyService {

private static final float DEFAULT_WALK_SPEED = 0.2F;
private final Map<UUID, Catboy> catboys = new HashMap<>();
private final EventCaller eventCaller;

private final CatBoyEntityService catBoyEntityService;
private final CatBoySettings catBoySettings;

@Inject
CatboyServiceImpl(EventCaller eventCaller) {
CatboyServiceImpl(
EventCaller eventCaller,
CatBoyEntityService catBoyEntityService,
CatBoySettings catBoySettings
) {
this.eventCaller = eventCaller;
this.catBoyEntityService = catBoyEntityService;
this.catBoySettings = catBoySettings;
}

@Override
public void markAsCatboy(Player player, Cat.Type type) {
Catboy catboy = new Catboy(player.getUniqueId(), type);
this.catboys.put(player.getUniqueId(), catboy);

Cat entity = (Cat) player.getWorld().spawnEntity(player.getLocation(), EntityType.CAT);
entity.setInvulnerable(true);
entity.setOwner(player);
entity.setCatType(type);

player.addPassenger(entity);
player.setWalkSpeed(0.4F);
Cat cat = this.catBoyEntityService.createCatboyEntity(player, type);
player.addPassenger(cat);
player.setWalkSpeed(this.catBoySettings.getCatboyWalkSpeed());

this.eventCaller.callEvent(new CatboySwitchEvent(player, true));
}
Expand All @@ -48,7 +52,7 @@ public void unmarkAsCatboy(Player player) {
this.catboys.remove(player.getUniqueId());
player.getPassengers().forEach(entity -> entity.remove());
player.getPassengers().clear();
player.setWalkSpeed(0.2F);
player.setWalkSpeed(DEFAULT_WALK_SPEED);

this.eventCaller.callEvent(new CatboySwitchEvent(player, false));
}
Expand Down Expand Up @@ -90,5 +94,4 @@ public Optional<Catboy> getCatboy(UUID uuid) {
public Collection<Catboy> getCatboys() {
return Collections.unmodifiableCollection(this.catboys.values());
}

}

0 comments on commit 1198dfc

Please sign in to comment.