Skip to content

Commit

Permalink
Platform stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Dec 19, 2024
1 parent dc77b19 commit b6edc81
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import dev.mrshawn.deathmessages.listeners.customlisteners.BroadcastPlayerDeathListener;
import dev.mrshawn.deathmessages.utils.ComponentUtil;
import dev.mrshawn.deathmessages.utils.EventUtil;
import dev.mrshawn.deathmessages.utils.PlatformUtil;
import dev.mrshawn.deathmessages.utils.Updater;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class DeathMessages extends JavaPlugin {
public void onEnable() {
instance.adventure = BukkitAudiences.create(instance);

PlatformUtil.init();
initListeners();
initCommands();
getHooks().registerHooks();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.mrshawn.deathmessages.utils;

import dev.mrshawn.deathmessages.DeathMessages;

public class PlatformUtil {

//public static final boolean IS_FOLIA = DeathMessages.getInstance().foliaLib.isFolia();

public static void init() {
if (Util.isNewerAndEqual(20, 5)) {
throw new UnsupportedOperationException("You should use DeathMessagesModern for version >= 1.20.5");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.util.BoundingBox;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -109,9 +110,9 @@ public static boolean isNumeric(String s) {

public static void getExplosionNearbyEffected(Player p, Block b) {
List<UUID> effected = new ArrayList<>();
List<Entity> getNearby = new ArrayList<>(isNewerAndEqual(13, 0)
Collection<Entity> getNearby = isNewerAndEqual(13, 0)
? b.getWorld().getNearbyEntities(BoundingBox.of(b).expand(24)) // TODO: make it configurable
: b.getWorld().getNearbyEntities(b.getLocation(), 24, 24, 24));
: b.getWorld().getNearbyEntities(b.getLocation(), 24, 24, 24);

getNearby.forEach(ent -> {
if (ent instanceof Player) {
Expand Down Expand Up @@ -225,6 +226,10 @@ public static String getPlayerDisplayName(Player player) {
/*
Sakamoto Util
*/
// Note:
// In Modern module, any condition relates to <= 1.20.4 are removed
// In legacy module, any conditions relates to > 1.20.4 are removed
// Version support range can be found in README.md

// Server version, e.g. 1.20.2-R0.1-SNAPSHOT -> {"1","20","2"}
private final static String[] serverVersion = Bukkit.getServer().getBukkitVersion()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import dev.mrshawn.deathmessages.listeners.customlisteners.BroadcastPlayerDeathListener;
import dev.mrshawn.deathmessages.utils.ComponentUtil;
import dev.mrshawn.deathmessages.utils.EventUtil;
import dev.mrshawn.deathmessages.utils.PlatformUtil;
import dev.mrshawn.deathmessages.utils.Updater;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
Expand All @@ -48,6 +49,7 @@ public class DeathMessages extends JavaPlugin {

@Override
public void onEnable() {
PlatformUtil.init();
initListeners();
initCommands();
getHooks().registerHooks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void onEntityDamage(EntityDamageEvent e) {
getPlayer.ifPresent(pm -> {
pm.setLastDamageCause(e.getCause());

if (Util.isNewerAndEqual(20, 3) && e.getCause().equals(EntityDamageEvent.DamageCause.BLOCK_EXPLOSION)) {
if (e.getCause().equals(EntityDamageEvent.DamageCause.BLOCK_EXPLOSION)) {
// For >= 1.20.3, because TNT explosion became BLOCK_EXPLOSION since 1.20.3
if (e.getDamageSource().getDirectEntity() instanceof TNTPrimed) {
TNTPrimed tnt = (TNTPrimed) e.getDamageSource().getDirectEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void entityDamageByEntity(EntityDamageByEntityEvent e) {
pm.setLastEntityDamager(tnt.getSource());
}
pm.setLastExplosiveEntity(e.getDamager());
} else if (Util.isNewerAndEqual(16, 0) && e.getDamager() instanceof Firework) { // Firework extends Entity under <= 1.15
} else if (e.getDamager() instanceof Firework) { // Firework extends Entity under <= 1.15
Firework firework = (Firework) e.getDamager();
try {
if (firework.getShooter() instanceof LivingEntity) {
Expand Down Expand Up @@ -115,7 +115,7 @@ public void entityDamageByEntity(EntityDamageByEntityEvent e) {
getPlayer.ifPresent(em::setLastPlayerDamager);
}
em.setLastExplosiveEntity(e.getDamager());
} else if (Util.isNewerAndEqual(16, 0) && e.getDamager() instanceof Firework) {
} else if (e.getDamager() instanceof Firework) {
Firework firework = (Firework) e.getDamager();
try {
if (firework.getShooter() instanceof Player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onInteract(PlayerInteractEvent e) {
if (getBlock.getType().name().endsWith("BED") || getBlock.getType().name().endsWith("BED_BLOCK")) {
Util.getExplosionNearbyEffected(e.getPlayer(), getBlock);
}
} else if (Util.isNewerAndEqual(16, 0)) {
} else {
if (!getBlock.getWorld().getEnvironment().equals(World.Environment.NETHER)) {
if (getBlock.getType().equals(Material.RESPAWN_ANCHOR)) {
Util.getExplosionNearbyEffected(e.getPlayer(), getBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,14 @@ public static TextComponent[] playerNatureDeathMessage(PlayerManager pm, Player
if (explosion.get().getMaterial().name().contains("BED")) {
components[1] = Assets.getNaturalDeath(pm, "Bed");
}
if (Util.isNewerAndEqual(16, 0)) {
if (explosion.get().getMaterial().equals(Material.RESPAWN_ANCHOR)) {
components[1] = Assets.getNaturalDeath(pm, "Respawn-Anchor");
}
if (explosion.get().getMaterial().equals(Material.RESPAWN_ANCHOR)) {
components[1] = Assets.getNaturalDeath(pm, "Respawn-Anchor");
}
// Dreeam TODO: Check weather needs to handle unknow explosion to prevent potential empty death message
}
} else if (pm.getLastDamage().equals(EntityDamageEvent.DamageCause.PROJECTILE)) {
components[1] = Assets.getNaturalDeath(pm, Assets.getSimpleProjectile(pm.getLastProjectileEntity()));
} else if (Util.isNewerAndEqual(9, 0) && Util.isOlderAndEqual(999, 999) && pm.getLastEntityDamager() instanceof AreaEffectCloud) { // Fix MC-84595 - Killed by Dragon's Breath
} else if (Util.isOlderAndEqual(999, 999) && pm.getLastEntityDamager() instanceof AreaEffectCloud) { // Fix MC-84595 - Killed by Dragon's Breath
AreaEffectCloud cloud = (AreaEffectCloud) pm.getLastEntityDamager();

if (cloud.getSource() instanceof EnderDragon) {
Expand Down Expand Up @@ -151,11 +149,9 @@ public static TextComponent[] playerDeathMessage(PlayerManager pm, boolean gang)
}

// Respawn Anchor kill
if (Util.isNewerAndEqual(16, 0)) {
if (explosionManager.get().getMaterial().equals(Material.RESPAWN_ANCHOR)) {
components[1] = get(gang, pm, pyro.get().getPlayer(), "Respawn-Anchor");
return components;
}
if (explosionManager.get().getMaterial().equals(Material.RESPAWN_ANCHOR)) {
components[1] = get(gang, pm, pyro.get().getPlayer(), "Respawn-Anchor");
return components;
}
}
}
Expand Down Expand Up @@ -240,11 +236,9 @@ public static TextComponent[] entityDeathMessage(EntityManager em, MobType mobTy
}

// Respawn Anchor kill
if (Util.isNewerAndEqual(16, 0)) {
if (explosionManager.get().getMaterial().equals(Material.RESPAWN_ANCHOR)) {
components[1] = getEntityDeath(pyro.get().getPlayer(), em.getEntity(), "Respawn-Anchor", mobType);
return components;
}
if (explosionManager.get().getMaterial().equals(Material.RESPAWN_ANCHOR)) {
components[1] = getEntityDeath(pyro.get().getPlayer(), em.getEntity(), "Respawn-Anchor", mobType);
return components;
}
}
}
Expand Down Expand Up @@ -302,9 +296,7 @@ public static TextComponent getNaturalDeath(PlayerManager pm, String damageCause
if (msg.contains("%block%") && pm.getLastEntityDamager() instanceof FallingBlock) {
try {
FallingBlock fb = (FallingBlock) pm.getLastEntityDamager();
String material = Util.isNewerAndEqual(12, 0)
? fb.getBlockData().getMaterial().toString().toLowerCase()
: fb.getMaterial().toString().toLowerCase();
String material = fb.getBlockData().getMaterial().toString().toLowerCase();
String configValue = Messages.getInstance().getConfig().getString("Blocks." + material);

base.append(Util.convertFromLegacy(msg.replaceAll("%block%", configValue)));
Expand All @@ -330,10 +322,8 @@ public static TextComponent getNaturalDeath(PlayerManager pm, String damageCause
if (!i.getType().equals(XMaterial.BOW.parseMaterial())) {
return getNaturalDeath(pm, "Projectile-Unknown");
}
if (Util.isNewerAndEqual(14, 0)) {
if (!i.getType().equals(XMaterial.CROSSBOW.parseMaterial())) {
return getNaturalDeath(pm, "Projectile-Unknown");
}
if (!i.getType().equals(XMaterial.CROSSBOW.parseMaterial())) {
return getNaturalDeath(pm, "Projectile-Unknown");
}

Component displayName;
Expand Down Expand Up @@ -1067,16 +1057,11 @@ private static Component getI18nName(ItemStack i, Player p) {
Component i18nName;

if (Settings.getInstance().getConfig().getBoolean(Config.DISPLAY_I18N_ITEM_NAME.getPath()) && !DeathMessages.getHooks().discordSRVEnabled) {
if (Util.isNewerAndEqual(12, 0)) {
// Block: block.minecraft.example
// Item: item.minecraft.example
String materialType = i.getType().isBlock() ? "block" : "item";
String rawTranslatable = "<lang:" + materialType + ".minecraft." + i.getType().name().toLowerCase() + ">";
i18nName = MiniMessage.miniMessage().deserialize(rawTranslatable);
} else {
String name = capitalize(i.getType().name());
i18nName = Component.text(name);
}
// Block: block.minecraft.example
// Item: item.minecraft.example
String materialType = i.getType().isBlock() ? "block" : "item";
String rawTranslatable = "<lang:" + materialType + ".minecraft." + i.getType().name().toLowerCase() + ">";
i18nName = MiniMessage.miniMessage().deserialize(rawTranslatable);
} else {
String name = capitalize(i.getType().name());
i18nName = Component.text(name);
Expand All @@ -1093,14 +1078,9 @@ private static Component getI18nName(Entity mob, Player p) {
Component i18nName;

if (Settings.getInstance().getConfig().getBoolean(Config.DISPLAY_I18N_MOB_NAME.getPath()) && !DeathMessages.getHooks().discordSRVEnabled) {
if (Util.isNewerAndEqual(12, 0)) {
// Entity: entity.minecraft.example
String rawTranslatable = "<lang:entity.minecraft." + mob.getType().name().toLowerCase() + ">";
i18nName = MiniMessage.miniMessage().deserialize(rawTranslatable);
} else {
String name = capitalize(mob.getType().name());
i18nName = Component.text(name);
}
// Entity: entity.minecraft.example
String rawTranslatable = "<lang:entity.minecraft." + mob.getType().name().toLowerCase() + ">";
i18nName = MiniMessage.miniMessage().deserialize(rawTranslatable);
} else {
String name = capitalize(mob.getType().name());
i18nName = Component.text(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static Component buildItemHover(Player player, ItemStack i, Component dis
i = DeathMessages.getHooks().ecoExtension.getEcoEnchantsItem(i, player);
}

// For >= 1.20.5
HoverEvent<HoverEvent.ShowItem> showItem = i.asHoverEvent();

return displayName.hoverEvent(showItem);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.mrshawn.deathmessages.utils;

import dev.mrshawn.deathmessages.DeathMessages;

public class PlatformUtil {

//public static final boolean IS_FOLIA = DeathMessages.getInstance().foliaLib.isFolia();

public static void init() {
if (Util.isOlderAndEqual(20, 4)) {
throw new UnsupportedOperationException("You should use DeathMessagesLegacy for version <= 1.20.4");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.util.BoundingBox;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -75,24 +76,22 @@ public static String convertToLegacy(Component component) {
To support both bungee and adventure RGB color code in legacy
*/
private static String colorizeBungeeRGB(String s) {
if (isNewerAndEqual(16, 0)) {
// Match bungee RGB color code only, use Negative Lookbehind to avoid matching code begin with &
Pattern pattern = Pattern.compile("(?<!&)(#[0-9a-fA-F]{6})");
Matcher matcher = pattern.matcher(s);
StringBuffer result = new StringBuffer();
// Match bungee RGB color code only, use Negative Lookbehind to avoid matching code begin with &
Pattern pattern = Pattern.compile("(?<!&)(#[0-9a-fA-F]{6})");
Matcher matcher = pattern.matcher(s);
StringBuffer result = new StringBuffer();

while (matcher.find()) {
String colorCode = matcher.group(1);
while (matcher.find()) {
String colorCode = matcher.group(1);

String replacement = "&" + colorCode;
matcher.appendReplacement(result, replacement);
}
String replacement = "&" + colorCode;
matcher.appendReplacement(result, replacement);
}

// Append rest of string
matcher.appendTail(result);
// Append rest of string
matcher.appendTail(result);

s = result.toString();
}
s = result.toString();

return s;
}
Expand All @@ -109,9 +108,7 @@ public static boolean isNumeric(String s) {

public static void getExplosionNearbyEffected(Player p, Block b) {
List<UUID> effected = new ArrayList<>();
List<Entity> getNearby = new ArrayList<>(isNewerAndEqual(13, 0)
? b.getWorld().getNearbyEntities(BoundingBox.of(b).expand(24)) // TODO: make it configurable
: b.getWorld().getNearbyEntities(b.getLocation(), 24, 24, 24));
Collection<Entity> getNearby = b.getWorld().getNearbyEntities(BoundingBox.of(b).expand(24)); // TODO: make it configurable

getNearby.forEach(ent -> {
if (ent instanceof Player) {
Expand Down Expand Up @@ -243,6 +240,10 @@ public static Component getPlayerDisplayNameComponent(Player player) {
/*
Sakamoto Util
*/
// Note:
// In Modern module, any condition relates to <= 1.20.4 are removed
// In legacy module, any conditions relates to > 1.20.4 are removed
// Version support range can be found in README.md

// Server version, e.g. 1.20.2-R0.1-SNAPSHOT -> {"1","20","2"}
private final static String[] serverVersion = Bukkit.getServer().getBukkitVersion()
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ Please feel free to contribute or modify existing translations if they are not r

## TODOs

> - Look into get entity name
> - Custom bstats charts (Pending)
> - PVPManager support
> - Actionbar message and private Actionbar message
> - Check getBiome name in assets
Expand Down

0 comments on commit b6edc81

Please sign in to comment.