-
-
Notifications
You must be signed in to change notification settings - Fork 25
GH-1420 Add system where mobs ignore players #1420
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||||
| package com.eternalcode.core.feature.notarget; | ||||||
|
|
||||||
| import java.util.UUID; | ||||||
| import org.bukkit.entity.Player; | ||||||
|
|
||||||
| public interface MobTargetService { | ||||||
|
|
||||||
| /* | ||||||
| * Removes mob tracking for player. | ||||||
| */ | ||||||
| void removeTracking(Player player); | ||||||
|
|
||||||
| /* | ||||||
| * Checks if a player is ignored for mob targeting. | ||||||
| * @return true if the player is ignored, false otherwise. | ||||||
| */ | ||||||
| boolean doMobsIgnore(UUID uniqueId); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i suggested isIgnored because the "mobs" part is already in class name and i don't think we should duplicate it. |
||||||
|
|
||||||
| /* | ||||||
| * Removes a player from the mob targeting ignore list. | ||||||
| */ | ||||||
| void startTracking(UUID uniqueId); | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.eternalcode.core.feature.notarget; | ||
|
|
||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.injector.annotations.component.Controller; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.event.entity.EntityTargetLivingEntityEvent; | ||
|
|
||
| @Controller | ||
| public class MobTargetListener implements Listener { | ||
|
|
||
| private final MobTargetService mobTargetService; | ||
|
|
||
| @Inject | ||
| public MobTargetListener(MobTargetService mobTargetService) { | ||
| this.mobTargetService = mobTargetService; | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onMobTarget(EntityTargetLivingEntityEvent event) { | ||
| if (event.getTarget() instanceof Player player && this.mobTargetService.doMobsIgnore(player.getUniqueId())) { | ||
| event.setCancelled(true); | ||
| } | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.eternalcode.core.feature.notarget; | ||
|
|
||
| import com.eternalcode.core.injector.annotations.component.Service; | ||
| import java.util.HashSet; | ||
| import java.util.UUID; | ||
| import org.bukkit.entity.Entity; | ||
| import org.bukkit.entity.Mob; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| @Service | ||
| public class MobTargetServiceImpl implements MobTargetService { | ||
|
|
||
| private final HashSet<UUID> mobTargetMap = new HashSet<>(); | ||
|
|
||
| @Override | ||
| public void removeTracking(Player player) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stopTracking would be better imo because we have startTracking so these names naturally compliment themselves |
||
| this.mobTargetMap.add(player.getUniqueId()); | ||
|
|
||
| this.enableNoTarget(player); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean doMobsIgnore(UUID uniqueId) { | ||
| return this.mobTargetMap.contains(uniqueId); | ||
| } | ||
|
|
||
| @Override | ||
| public void startTracking(UUID uniqueId) { | ||
| this.mobTargetMap.remove(uniqueId); | ||
| } | ||
|
|
||
| private void enableNoTarget(Player player) { | ||
| for (Entity entity : player.getNearbyEntities(30, 30, 30)) { | ||
| if (entity instanceof Mob mob) { | ||
|
|
||
| if (mob.getTarget() != null && mob.getTarget().equals(player)) { | ||
| mob.setTarget(null); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package com.eternalcode.core.feature.notarget; | ||
|
|
||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.notice.NoticeService; | ||
| import dev.rollczi.litecommands.annotations.argument.Arg; | ||
| import dev.rollczi.litecommands.annotations.command.Command; | ||
| import dev.rollczi.litecommands.annotations.context.Sender; | ||
| import dev.rollczi.litecommands.annotations.execute.Execute; | ||
| import dev.rollczi.litecommands.annotations.permission.Permission; | ||
| import java.util.UUID; | ||
| import org.bukkit.command.CommandSender; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| @Command(name = "notarget") | ||
| @Permission("eternalcore.notarget") | ||
| public class NoTargetCommand { | ||
|
|
||
| private final MobTargetService mobTargetService; | ||
| private final NoticeService noticeService; | ||
|
|
||
| @Inject | ||
| public NoTargetCommand(MobTargetService mobTargetService, NoticeService noticeService) { | ||
| this.mobTargetService = mobTargetService; | ||
| this.noticeService = noticeService; | ||
| } | ||
|
|
||
| @Execute | ||
| void execute(@Sender Player player) { | ||
| UUID uniqueId = player.getUniqueId(); | ||
|
|
||
| if (this.mobTargetService.doMobsIgnore(uniqueId)) { | ||
| this.turnOff(uniqueId); | ||
| return; | ||
| } | ||
|
CitralFlo marked this conversation as resolved.
|
||
|
|
||
| this.turnOn(player); | ||
| } | ||
|
|
||
| @Execute | ||
| void execute(@Sender CommandSender sender, @Arg Player target) { | ||
| UUID uniqueId = target.getUniqueId(); | ||
|
|
||
| if (this.mobTargetService.doMobsIgnore(uniqueId)) { | ||
| this.noticeService.create() | ||
| .notice(translation -> translation.noTarget().turnedOn()) | ||
| .placeholder("{PLAYER}", target.getName()) | ||
| .sender(sender) | ||
| .send(); | ||
|
|
||
| this.turnOn(target); | ||
| return; | ||
| } | ||
|
CitralFlo marked this conversation as resolved.
|
||
| this.noticeService.create() | ||
| .notice(translation -> translation.noTarget().turnedOff()) | ||
| .placeholder("{PLAYER}", target.getName()) | ||
| .sender(sender) | ||
| .send(); | ||
|
|
||
| this.turnOff(uniqueId); | ||
| } | ||
|
|
||
| private void turnOn(Player player) { | ||
| this.mobTargetService.removeTracking(player); | ||
|
|
||
| this.noticeService.create() | ||
| .notice(translation -> translation.noTarget().enabled()) | ||
| .player(player.getUniqueId()) | ||
| .send(); | ||
| } | ||
|
|
||
| private void turnOff(UUID uniqueId) { | ||
| this.mobTargetService.startTracking(uniqueId); | ||
|
|
||
| this.noticeService.create() | ||
| .notice(translation -> translation.noTarget().disabled()) | ||
| .player(uniqueId) | ||
| .send(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||
| package com.eternalcode.core.feature.notarget.messages; | ||||||
|
|
||||||
| import com.eternalcode.multification.notice.Notice; | ||||||
| import eu.okaeri.configs.OkaeriConfig; | ||||||
| import eu.okaeri.configs.annotation.Comment; | ||||||
| import lombok.Getter; | ||||||
| import lombok.experimental.Accessors; | ||||||
|
|
||||||
| @Getter | ||||||
| @Accessors(fluent = true) | ||||||
| public class ENNoTargetMessages extends OkaeriConfig implements NoTargetMessages { | ||||||
| Notice disabled = Notice.chat("<color:#9d6eef>► <white>Monsters will target You!"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Notice enabled = Notice.chat("<color:#9d6eef>► <white>Monsters will no longer target You!"); | ||||||
|
|
||||||
| @Comment({" ", "# Placeholder: {PLAYER} - Targeted player name"}) | ||||||
| Notice turnedOff = Notice.chat("<color:#9d6eef>► <white>Monsters will target <green>{PLAYER}<white> - protection has been turned off!"); | ||||||
| Notice turnedOn = Notice.chat("<color:#9d6eef>► <white>Monsters will ignore <green>{PLAYER}<white> - protection has been turned on!"); | ||||||
|
|
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.eternalcode.core.feature.notarget.messages; | ||
|
|
||
| import com.eternalcode.multification.notice.Notice; | ||
|
|
||
| public interface NoTargetMessages { | ||
| Notice enabled(); | ||
| Notice disabled(); | ||
|
|
||
| Notice turnedOn(); | ||
| Notice turnedOff(); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||
| package com.eternalcode.core.feature.notarget.messages; | ||||||
|
|
||||||
| import com.eternalcode.multification.notice.Notice; | ||||||
| import eu.okaeri.configs.OkaeriConfig; | ||||||
| import eu.okaeri.configs.annotation.Comment; | ||||||
| import lombok.Getter; | ||||||
| import lombok.experimental.Accessors; | ||||||
|
|
||||||
| @Getter | ||||||
| @Accessors(fluent = true) | ||||||
| public class PLNoTargetMessages extends OkaeriConfig implements NoTargetMessages { | ||||||
| Notice disabled = Notice.chat("<color:#9d6eef>► <white>Potwory nie będą Cię więcej ignorować!"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Notice enabled = Notice.chat("<color:#9d6eef>► <white>Potwory będą Cię ignorować!"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| @Comment({ " ", "# Placeholder: {PLAYER} - Wybrany komendą gracz"}) | ||||||
| Notice turnedOff = Notice.chat("<color:#9d6eef>► <white>Potwory nie będą ignorować gracza: <green>{PLAYER}<white> - ochrona została wyłączona!"); | ||||||
| Notice turnedOn = Notice.chat("<color:#9d6eef>► <white>Potwory będą ignorować gracza: <green>{PLAYER}<white> - ochrona została włączona!"); | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.