Skip to content

Commit

Permalink
Still support achievements when using versions below 1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed May 25, 2017
1 parent 979035b commit 7611d26
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ settings:
death: '%message%'
players-list: 'Online (%playerCount%/%maxPlayers%): %message%'
players-nobody: 'Nobody is minecrafting right now.'
advancement: '%sender% earned advancement %green%[%message%]'
achievement: '%sender% earned achievement %green%[%message%]'
from-irc:
chat: '[%srcChannel%] <%sender%> %message%'
private: '%sender% whispers> %message%'
Expand Down Expand Up @@ -291,7 +291,7 @@ default-attributes:
say: true
generic: true
death: true
advancement: true
achievement: true
mode: false
notice: false

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ensifera.animosity.craftirc;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;

final class AdvancementsListener implements Listener {
private final CraftIRC plugin;

AdvancementsListener(CraftIRC plugin) {
this.plugin = plugin;
}

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerAdvancement(PlayerAdvancementDoneEvent event) {
if (this.plugin.isHeld(CraftIRC.HoldType.ADVANCEMENTS)) {
return;
}
if (event.getAdvancement() == null) {
return;
}
final RelayedMessage msg = this.plugin.newMsg(this.plugin.getEndPoint(this.plugin.cMinecraftTag()), null, "achievement");
if (msg == null) {
return;
}
msg.setField("sender", event.getPlayer().getDisplayName());
msg.setField("message", event.getAdvancement().getKey().getKey());
msg.setField("world", event.getPlayer().getWorld().getName());
msg.setField("realSender", event.getPlayer().getName());
msg.setField("prefix", this.plugin.getPrefix(event.getPlayer()));
msg.setField("suffix", this.plugin.getSuffix(event.getPlayer()));
msg.doNotColor("message");
msg.post();
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/ensifera/animosity/craftirc/CraftIRC.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.ensifera.animosity.craftirc.libs.com.sk89q.util.config.ConfigurationNode;
import com.ensifera.animosity.craftirc.libs.org.jibble.pircbot.Colors;
import net.milkbowl.vault.chat.Chat;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -78,6 +79,19 @@ private void logDerp(String message) {
}
}

private static final Pattern versionPattern = Pattern.compile("(\\d).(\\d+).*");
private boolean versionGreaterorEqualThan(int major, int minor) {
String bukkitVersion = Bukkit.getBukkitVersion();
Matcher versionMatcher = versionPattern.matcher(bukkitVersion);
if (versionMatcher.find()) {
int majorV = Integer.parseInt(versionMatcher.group(1));
int minorV = Integer.parseInt(versionMatcher.group(2));
if (majorV > major || (majorV == major && minorV >= minor))
return true;
}
return false;
}

/***************************
* Bukkit stuff
***************************/
Expand Down Expand Up @@ -148,6 +162,9 @@ public void onEnable() {

// Event listeners
this.getServer().getPluginManager().registerEvents(new CraftIRCListener(this), this);
// Check for advancements api
if (this.versionGreaterorEqualThan(1, 12))
this.getServer().getPluginManager().registerEvents(new AdvancementsListener(this), this);
this.getServer().getPluginManager().registerEvents(new ConsoleListener(this), this);

// Native endpoints!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.bukkit.event.player.PlayerAchievementAwardedEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
Expand Down Expand Up @@ -173,19 +173,19 @@ public void onPlayerDeath(PlayerDeathEvent event) {
}

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerAdvancement(PlayerAdvancementDoneEvent event) {
public void onPlayerAchievement(PlayerAchievementAwardedEvent event) {
if (this.plugin.isHeld(CraftIRC.HoldType.ADVANCEMENTS)) {
return;
}
if (event.getAdvancement() == null) {
if (event.getAchievement() == null) {
return;
}
final RelayedMessage msg = this.plugin.newMsg(this.plugin.getEndPoint(this.plugin.cMinecraftTag()), null, "advancement");
final RelayedMessage msg = this.plugin.newMsg(this.plugin.getEndPoint(this.plugin.cMinecraftTag()), null, "achievement");
if (msg == null) {
return;
}
msg.setField("sender", event.getPlayer().getDisplayName());
msg.setField("message", event.getAdvancement().getKey().getKey());
msg.setField("message", event.getAchievement().name());
msg.setField("world", event.getPlayer().getWorld().getName());
msg.setField("realSender", event.getPlayer().getName());
msg.setField("prefix", this.plugin.getPrefix(event.getPlayer()));
Expand Down

0 comments on commit 7611d26

Please sign in to comment.