Skip to content

Commit

Permalink
feat: support ModelEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
entrypointkr committed Apr 29, 2024
1 parent 5a9a899 commit 8d51cca
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ In gradle:

groovy
```
compileOnly 'io.typst:bukkit-glow:2.0.0'
compileOnly 'io.typst:bukkit-glow:2.1.0'
```

kts
```
compileOnly("io.typst:bukkit-glow:2.0.0")
compileOnly("io.typst:bukkit-glow:2.1.0")
```

In plugin.yml:
Expand All @@ -29,3 +29,6 @@ GlowAPI glow = new GlowAPI(); // No cost for construction
glow.setGlowing(receiver, target, color);
glow.removeGlowing(receiver, target);
```

## Support plugins
- [ModelEngine](https://git.mythiccraft.io/mythiccraft/model-engine-4/-/wikis/Modeling)
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ plugins {
}

group = 'io.typst'
version = '2.0.0'
version = '2.1.0'

repositories {
mavenCentral()
maven { url "https://repo.papermc.io/repository/maven-public/" }
maven { url 'https://jitpack.io' }
maven { url "https://repo.dmulloy2.net/repository/public/" }
maven { url 'https://mvn.lumine.io/repository/maven-public/' }
}

dependencies {
compileOnly spigot('1.20.2')
compileOnly fileTree(include: '*.jar', dir: 'libs')
compileOnly lombok('1.18.32')
compileOnly protocolLib('5.1.0')
compileOnly 'com.ticxo.modelengine:ModelEngine:R4.0.6'
annotationProcessor lombok('1.18.32')
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
Expand All @@ -28,6 +30,7 @@ dependencies {
spigot {
name 'BukkitGlow'
depends 'ProtocolLib'
softDepends 'ModelEngine'
}

test {
Expand Down
37 changes: 33 additions & 4 deletions src/main/java/io/typst/bukkit/glow/GlowAPI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.typst.bukkit.glow;


import io.typst.bukkit.glow.modelengine.ModelEngineAccessor;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -11,23 +14,49 @@
import java.util.UUID;

public class GlowAPI {
public void setGlowing(Player target, Player receiver) {
setGlowing(target, receiver, ChatColor.WHITE);
}

private GlowService getServiceOrThrow() {
private static GlowService getServiceOrThrow() {
GlowService service = JavaPlugin.getPlugin(GlowPlugin.class).getGlow();
return Objects.requireNonNull(service);
}

private static boolean checkModelEngine() {
return Bukkit.getPluginManager().isPluginEnabled("ModelEngine");
}

// TODO: should be removed
public void setGlowing(Player target, Player receiver) {
setGlowing((Entity) target, receiver);
}

public void setGlowing(Entity target, Player receiver) {
setGlowing(target, receiver, ChatColor.WHITE);
}

// TODO: should be removed
public void setGlowing(Player target, Player receiver, ChatColor color) {
setGlowing((Entity) target, receiver, color);
}

public void setGlowing(Entity target, Player receiver, ChatColor color) {
GlowService service = getServiceOrThrow();
service.setGlowing(target, receiver, color);
if (checkModelEngine()) {
ModelEngineAccessor.setGlowing(target.getUniqueId(), color);
}
}

// TODO: should be removed
public void removeGlowing(Player target, Player receiver) {
removeGlowing((Entity) target, receiver);
}

public void removeGlowing(Entity target, Player receiver) {
GlowService service = getServiceOrThrow();
service.removeGlowing(target, receiver);
if (checkModelEngine()) {
ModelEngineAccessor.removeGlowing(target.getUniqueId());
}
}

public boolean hasGlowingView(UUID receiverId) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/typst/bukkit/glow/GlowEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.WrappedDataValue;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.ServicePriority;

import java.util.List;
import java.util.stream.Collectors;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/io/typst/bukkit/glow/GlowService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import org.bukkit.ChatColor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

Expand All @@ -28,16 +29,16 @@ public Optional<PlayerGlowData> getPlayerGlowData(String targetName, UUID receiv
.get(targetName));
}

public void setGlowing(Player target, Player receiver, ChatColor color) {
public void setGlowing(Entity target, Player receiver, ChatColor color) {
// update internal state
Map<String, PlayerGlowData> view = views.computeIfAbsent(receiver.getUniqueId(), k -> new HashMap<>());
// TODO: this name can be replaced by entity id?
view.put(target.getName(), new PlayerGlowData(target.getName(), color));

// send metadata packet
PacketContainer metadataPacket = GlowPackets.createGlowingMetadataPacket(target, true);
ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, metadataPacket);


// send team packet
// - create team packet
// - add player packet - X
Expand All @@ -50,7 +51,7 @@ public void setGlowing(Player target, Player receiver, ChatColor color) {
ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, teamCreationPacket);
}

public void removeGlowing(Player target, Player receiver) {
public void removeGlowing(Entity target, Player receiver) {
// update internal state
Map<String, PlayerGlowData> view = views.getOrDefault(receiver.getUniqueId(), Collections.emptyMap());
PlayerGlowData removedData = view.remove(target.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.typst.bukkit.glow.modelengine;

import com.ticxo.modelengine.api.ModelEngineAPI;
import com.ticxo.modelengine.api.model.ActiveModel;
import com.ticxo.modelengine.api.model.ModeledEntity;
import org.bukkit.ChatColor;

import java.util.UUID;

/**
* for lazy access due the soft depend
*/
public class ModelEngineAccessor {
public static void setGlowing(UUID ownerId, ChatColor color) {
ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(ownerId);
if (modeledEntity == null) {
return;
}
for (ActiveModel model : modeledEntity.getModels().values()) {
model.setGlowing(true);
model.setGlowColor((int) color.getChar());
}
}

public static void removeGlowing(UUID ownerId) {
ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(ownerId);
if (modeledEntity == null) {
return;
}
for (ActiveModel model : modeledEntity.getModels().values()) {
model.setGlowing(false);
}
}
}

0 comments on commit 8d51cca

Please sign in to comment.