Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stumper66 committed Aug 28, 2024
0 parents commit cc301e4
Show file tree
Hide file tree
Showing 13 changed files with 765 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# JetBrains IntelliJ
target/
.idea/
*.iml

# Eclipse
.classpath
.project
/.settings
/.idea/
.idea/jarRepositories.xml
.idea/discord.xml

# macOS
._.git
.DS_Store
75 changes: 75 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.stumper66</groupId>
<artifactId>VillagerAnnoucer</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>VillagerAnnouncer</name>
<description>Announces Villager Deaths</description>
<url>https://github.com/stumper66/VillagerAnnouncer</url>
<licenses>
<license>
<name>GNU AGPL v3.0</name>
<url>https://www.gnu.org/licenses/agpl-3.0.en.html</url>
</license>
</licenses>
<developers>
<developer>
<name>stumper66</name>
<url>https://github.com/stumper66</url>
</developer>
</developers>
<scm>
<url>https://github.com/stumper66/VillagerAnnouncer</url>
</scm>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<defaultGoal>clean package</defaultGoal>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
45 changes: 45 additions & 0 deletions src/io/github/stumper66/villagerannouncer/Commands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.github.stumper66.villagerannouncer;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class Commands implements CommandExecutor, TabCompleter {
public boolean onCommand(final @NotNull CommandSender sender, final @NotNull Command command, final @NotNull String label, final String @NotNull [] args) {
if (!sender.hasPermission("villagerannouncer")){
sender.sendMessage("Access denied");
return true;
}

if (args.length >= 1 && "reload".equalsIgnoreCase(args[0]))
doReload(sender);
else
sender.sendMessage("Villager Announcer " + VillagerAnnouncer.getInstance().getDescription().getVersion() +
"\nOptions: reload");

return true;
}

private void doReload(final @NotNull CommandSender sender){
if (!sender.hasPermission("villagerannouncer.reload")){
sender.sendMessage("Access denied");
return;
}

VillagerAnnouncer.getInstance().loadConfig();
sender.sendMessage("Reloaded the config");
}

@Override
public @Nullable List<String> onTabComplete(final @NotNull CommandSender sender, final @NotNull Command command, final @NotNull String label, final @NotNull String @NotNull [] args){
if (sender.hasPermission("villagerannouncer") && args.length == 1)
return List.of("reload");

return List.of();
}
}
102 changes: 102 additions & 0 deletions src/io/github/stumper66/villagerannouncer/EventListeners.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package io.github.stumper66.villagerannouncer;

import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityTransformEvent;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.time.Instant;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class EventListeners implements Listener {
public EventListeners(){
instance = this;
typesWeCareAbout = List.of(EntityType.VILLAGER, EntityType.ZOMBIE_VILLAGER);
entitiesThatHurtVillagers = new LinkedHashMap<>();
transformedVillagers = new HashSet<>();
}

private static EventListeners instance;
private final List<EntityType> typesWeCareAbout;
final Map<UUID, LivingEntity> entitiesThatHurtVillagers;
final Set<UUID> transformedVillagers;
private Instant lastEntryTime;

public static EventListeners getInstance(){
return instance;
}

@EventHandler(priority = EventPriority.NORMAL)
public void onEntityDeathEvent(final @NotNull EntityDeathEvent event){
if (!VillagerAnnouncer.getInstance().isEnabled) return;
if (!typesWeCareAbout.contains(event.getEntity().getType()))
return;

if (event.getEntity().getType() == EntityType.ZOMBIE_VILLAGER){
final boolean wasPreviouslyNormalVillager = event.getEntity().getPersistentDataContainer().has(
VillagerAnnouncer.getInstance().key,
PersistentDataType.INTEGER
);

if (!wasPreviouslyNormalVillager) return;
}

// the transform event occurs after the death event
// so we have to delay checking so the transform event can run first
final VillagerDeath vd = new VillagerDeath(event.getEntity());
Bukkit.getScheduler().runTaskLater(VillagerAnnouncer.getInstance(), vd::run, 20);
}

@EventHandler(priority = EventPriority.NORMAL)
public void onEntityDamageEvent(final @NotNull EntityDamageByEntityEvent event){
if (!VillagerAnnouncer.getInstance().isEnabled) return;
if (!typesWeCareAbout.contains(event.getEntity().getType())) return;
if (!(event.getDamager() instanceof LivingEntity damager)) return;

if (lastEntryTime != null){
final long howLongMS = Duration.between(lastEntryTime, Instant.now()).toMillis();
if (howLongMS >= 10000){
entitiesThatHurtVillagers.clear();
transformedVillagers.clear();
}
}

lastEntryTime = Instant.now();
entitiesThatHurtVillagers.put(event.getEntity().getUniqueId(), damager);
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
public void onEntityTransformEvent(final @NotNull EntityTransformEvent event){
if (!VillagerAnnouncer.getInstance().isEnabled) return;
if (event.getEntity().getType() != EntityType.VILLAGER) return;
if (event.getTransformReason() != EntityTransformEvent.TransformReason.INFECTION) return;

for (final Entity entity : event.getTransformedEntities()){
entity.getPersistentDataContainer().set(
VillagerAnnouncer.getInstance().key,
PersistentDataType.INTEGER,
1
);
}

lastEntryTime = Instant.now();
transformedVillagers.add(event.getEntity().getUniqueId());
final VillagerDeath villagerDeath = new VillagerDeath((LivingEntity) event.getEntity());
villagerDeath.wasInfected = true;
villagerDeath.run();
}
}
15 changes: 15 additions & 0 deletions src/io/github/stumper66/villagerannouncer/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.stumper66.villagerannouncer;

import java.util.logging.Logger;

public class Log {
private final static Logger log = Logger.getLogger("VillagerAnnouncer");

public static void inf(final String text){
log.info(text);
}

public static void war(final String text){
log.warning(text);
}
}
108 changes: 108 additions & 0 deletions src/io/github/stumper66/villagerannouncer/MessageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package io.github.stumper66.villagerannouncer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

/**
* This class contains a bunch of methods which
* make it very easy to translate '&'-based color
* codes in messages. You can colorize standard codes
* (&a, &b, &1, &2, etc), and even hex codes (&#abccdef),
* and also both in one method :)
*
* @author lokka30, Sullivan_Bognar, imDaniX
* @since 1.0.0
*/
public class MessageUtils {
private static boolean isRunningSpigot = true;

static {
isRunningSpigot = false;
try {
Class.forName("net.md_5.bungee.api.ChatColor");
isRunningSpigot = true;
} catch (ClassNotFoundException ignored) { }
}

/**
* Colorize a message, using '&' color codes - e.g. '&a' for ChatColor.GREEN.
* If the server is 1.16 or newer, then it will also translate hex codes - e.g. '&#abcdef'.
*
* @param msg the message to translate color codes from.
* @return the color-translated message.
* @author lokka30
* @see MessageUtils#colorizeHexCodes(String)
* @see MessageUtils#colorizeStandardCodes(String)
* @since unknown
*/
public static @NotNull String colorizeAll(String msg) {
return colorizeStandardCodes(colorizeHexCodes(msg));
}

/**
* This defaults the 'startTag' to '&#' and endTag to '' (nothing) to colorizeHexCodes.
*
* @param msg message to translate
* @return the translated string
* @author lokka30
* @see MessageUtils#colorizeHexCodes(String, String, String)
* @since unknown
*/
public static String colorizeHexCodes(String msg) {
return colorizeHexCodes("&#", "", msg);
}

/**
* (WARNING!) This does NOT colorize standard codes, ONLY hex codes.
* This translates all hex codes in a message. Hex codes are prefixed by '&#', e.g. '&#abcdef'.
* This method ensures the version is 1.16 or newer before translating - else, it will not translate the message.
*
* @author Elementeral @SpigotMC.org and imDaniX @ SpigotMC.org ~ <a href="https://www.spigotmc.org/threads/hex-color-code-translate.449748/#post-3867804">...</a>
*
* @param startTag what the tag should begin with - '&#' is recommended
* @param endTag what the tag should end with - '' (nothing) is recommended
* @param message the message that should be translated
* @return the translated string
*
* @since unknown
*/
public static String colorizeHexCodes(String startTag, String endTag, String message) {
if (!isRunningSpigot) return message;

final Pattern hexPattern = Pattern.compile(startTag + "([A-Fa-f0-9]{6})" + endTag);
Matcher matcher = hexPattern.matcher(message);
StringBuilder buffer = new StringBuilder(message.length() + 4 * 8);
final char colorChar = net.md_5.bungee.api.ChatColor.COLOR_CHAR;

while (matcher.find()) {
String group = matcher.group(1);
matcher.appendReplacement(buffer, colorChar + "x"
+ colorChar + group.charAt(0) + colorChar + group.charAt(1)
+ colorChar + group.charAt(2) + colorChar + group.charAt(3)
+ colorChar + group.charAt(4) + colorChar + group.charAt(5)
);
}
return matcher.appendTail(buffer).toString();
}

/**
* This does NOT colorize hex codes, ONLY standard codes.
* This translated all standard codes in a message. Standard codes are prefixed by '&', e.g. '&a'.
*
* @author lokka30
*
* @param msg the message to translate standard color codes from.
* @return the color-translated message.
*
* @since unknown
*/
public static @NotNull String colorizeStandardCodes(String msg) {
if (Bukkit.getName().equalsIgnoreCase("CraftBukkit"))
return org.bukkit.ChatColor.translateAlternateColorCodes('&', msg);
else
return net.md_5.bungee.api.ChatColor.translateAlternateColorCodes('&', msg);
}
}
Loading

0 comments on commit cc301e4

Please sign in to comment.