generated from Matt-MX/KtPaperPluginTemplate
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔥 Impl trait system and debug testing
- Loading branch information
Showing
9 changed files
with
190 additions
and
15 deletions.
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
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
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
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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/mattmx/nametags/entity/TestImplTrait.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,44 @@ | ||
package com.mattmx.nametags.entity; | ||
|
||
import com.mattmx.nametags.NameTags; | ||
import com.mattmx.nametags.entity.trait.Trait; | ||
import com.mattmx.nametags.event.NameTagEntityCreateEvent; | ||
import net.kyori.adventure.text.format.NamedTextColor; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.scheduler.BukkitTask; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class TestImplTrait implements Listener { | ||
|
||
@EventHandler | ||
public void onNameTagCreate(@NotNull NameTagEntityCreateEvent event) { | ||
event.getNameTag() | ||
.getTraits() | ||
.getOrAddTrait(RefreshTrait.class, RefreshTrait::new); | ||
} | ||
|
||
static class RefreshTrait extends Trait { | ||
private int i = 0; | ||
private boolean cancel = false; | ||
private final @NotNull BukkitTask task = Bukkit.getScheduler() | ||
.runTaskTimerAsynchronously(NameTags.getInstance(), () -> { | ||
if (cancel) return; | ||
|
||
getTag().modify((meta) -> { | ||
meta.setText(getTag().getBukkitEntity() | ||
.name() | ||
.color(i++ % 2 == 0 ? NamedTextColor.RED : NamedTextColor.GREEN)); | ||
}); | ||
|
||
}, 0L, 20L); | ||
|
||
@Override | ||
public void onDestroy() { | ||
cancel = true; | ||
task.cancel(); | ||
} | ||
} | ||
|
||
} |
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,23 @@ | ||
package com.mattmx.nametags.entity.trait; | ||
|
||
import com.mattmx.nametags.entity.NameTagEntity; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public abstract class Trait { | ||
private @Nullable NameTagEntity nameTag; | ||
|
||
public void setNameTag(@NotNull NameTagEntity tag) { | ||
this.nameTag = tag; | ||
} | ||
|
||
public @NotNull NameTagEntity getTag() { | ||
assert nameTag != null; | ||
return nameTag; | ||
} | ||
|
||
public void onDestroy() { | ||
|
||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/mattmx/nametags/entity/trait/TraitHolder.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,55 @@ | ||
package com.mattmx.nametags.entity.trait; | ||
|
||
import com.mattmx.nametags.entity.NameTagEntity; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.function.Supplier; | ||
|
||
public class TraitHolder { | ||
private final @NotNull NameTagEntity owner; | ||
private final @NotNull HashMap<Class<?>, Trait> map = new HashMap<>(); | ||
|
||
public TraitHolder(@NotNull NameTagEntity owner) { | ||
this.owner = owner; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends Trait> @Nullable T getTrait(@NotNull Class<T> traitClazz) { | ||
Trait trait = map.get(traitClazz); | ||
|
||
if (trait != null) { | ||
return (T) trait; | ||
} | ||
return null; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends Trait> @NotNull T getOrAddTrait(@NotNull Class<T> traitClazz, @NotNull Supplier<T> supplier) { | ||
return (T) map.computeIfAbsent(traitClazz, (k) -> { | ||
T trait = supplier.get(); | ||
trait.setNameTag(owner); | ||
return trait; | ||
}); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends Trait> @Nullable T removeTrait(@NotNull Class<T> traitClazz) { | ||
Trait trait = map.remove(traitClazz); | ||
|
||
if (trait != null) { | ||
trait.onDestroy(); | ||
return (T) trait; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public void destroy() { | ||
for (Trait trait : map.values()) { | ||
trait.onDestroy(); | ||
} | ||
map.clear(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/mattmx/nametags/event/NameTagEntityCreateEvent.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,28 @@ | ||
package com.mattmx.nametags.event; | ||
|
||
import com.mattmx.nametags.entity.NameTagEntity; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.event.HandlerList; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class NameTagEntityCreateEvent extends Event { | ||
private static final HandlerList handlers = new HandlerList(); | ||
private final @NotNull NameTagEntity nameTag; | ||
|
||
public NameTagEntityCreateEvent(@NotNull NameTagEntity nameTag) { | ||
this.nameTag = nameTag; | ||
} | ||
|
||
public @NotNull NameTagEntity getNameTag() { | ||
return nameTag; | ||
} | ||
|
||
public static HandlerList getHandlerList() { | ||
return handlers; | ||
} | ||
|
||
@Override | ||
public @NotNull HandlerList getHandlers() { | ||
return getHandlerList(); | ||
} | ||
} |
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