Skip to content

Commit

Permalink
Added a way to display hardcore hearts.
Browse files Browse the repository at this point in the history
ProtocolLib needed.
  • Loading branch information
AmauryCarrade committed Aug 28, 2014
1 parent 6683772 commit 28183be
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 2 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<id>dynmap-repo</id>
<url>http://repo.mikeprimm.com/</url>
</repository>
<repository>
<id>comphenix-rep</id>
<name>Comphenix Repository</name>
<url>http://repo.comphenix.net/content/groups/public</url>
</repository>
<repository>
<id>carrade-repo</id>
<url>http://depot.carrade.eu/maven2/</url>
Expand Down Expand Up @@ -63,5 +68,10 @@
<artifactId>dynmap-api</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
</project>
15 changes: 15 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import me.azenet.UHPlugin.i18n.I18n;
import me.azenet.UHPlugin.integration.UHDynmapIntegration;
import me.azenet.UHPlugin.integration.UHProtocolLibIntegrationWrapper;
import me.azenet.UHPlugin.integration.UHSpectatorPlusIntegration;
import me.azenet.UHPlugin.integration.UHWorldBorderIntegration;
import me.azenet.UHPlugin.listeners.UHCraftingListener;
Expand All @@ -43,6 +44,7 @@ public final class UHPlugin extends JavaPlugin {
private UHWorldBorderIntegration wbintegration = null;
private UHSpectatorPlusIntegration spintegration = null;
private UHDynmapIntegration dynmapintegration = null;
private UHProtocolLibIntegrationWrapper protocollibintegrationwrapper = null;

private I18n i18n = null;

Expand All @@ -68,6 +70,10 @@ public void onEnable() {
spintegration = new UHSpectatorPlusIntegration(this);
dynmapintegration = new UHDynmapIntegration(this);

// Needed to avoid a NoClassDefFoundError.
// I don't like this way of doing this, but else, the plugin will not load without ProtocolLib.
protocollibintegrationwrapper = new UHProtocolLibIntegrationWrapper(this);

commandManager = new UHPluginCommand(this);
getCommand("uh").setExecutor(commandManager);
getCommand("uh").setTabCompleter(new UHTabCompleter(this));
Expand Down Expand Up @@ -182,6 +188,15 @@ public UHDynmapIntegration getDynmapIntegration() {
return dynmapintegration;
}

/**
* Returns a wrapper of the representation of the ProtocolLib integration in the plugin.
*
* @return
*/
public UHProtocolLibIntegrationWrapper getProtocolLibIntegrationWrapper() {
return protocollibintegrationwrapper;
}


/**
* Returns the internationalization manager.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Plugin UltraHardcore (UHPlugin)
* Copyright (C) 2013 azenet
* Copyright (C) 2014 Amaury Carrade
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/].
*/

package me.azenet.UHPlugin.integration;

import java.util.ArrayList;
import java.util.List;

import me.azenet.UHPlugin.UHPlugin;
import me.azenet.UHPlugin.listeners.UHLoginPacketsListener;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;


public class UHProtocolLibIntegration {

private UHPlugin p = null;
private ProtocolManager pm = null;

public UHProtocolLibIntegration(UHPlugin p) {
this.p = p;

Plugin plTest = Bukkit.getServer().getPluginManager().getPlugin("ProtocolLib");
if(plTest == null || !plTest.isEnabled()) {
this.p.getLogger().warning("ProtocolLib is not present, so the integration was disabled.");
return;
}


this.pm = ProtocolLibrary.getProtocolManager();

if(isProtocolLibIntegrationEnabled() && p.getConfig().getBoolean("hardcore-hearts")) {
pm.addPacketListener(new UHLoginPacketsListener(p));
}

this.p.getLogger().info("Successfully hooked into ProtocolLib.");
}

public boolean isProtocolLibIntegrationEnabled() {
return (this.pm != null);
}

/**
* Checks if there are some enabled option which require ProtocolLib.
*
* @return A list of enabled options which requires ProtocolLib, or null
* if there isn't any enabled option that requires ProtocolLib.
*/
public List<String> isProtocolLibNeeded() {

ArrayList<String> options = new ArrayList<String>();
options.add("hardcore-hearts");

ArrayList<String> enabledOptions = new ArrayList<String>();

for(String option : options) {
if(p.getConfig().getBoolean(option)) {
enabledOptions.add(option);
}
}

if(enabledOptions.size() != 0) {
return enabledOptions;
}
else {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Plugin UltraHardcore (UHPlugin)
* Copyright (C) 2013 azenet
* Copyright (C) 2014 Amaury Carrade
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/].
*/

package me.azenet.UHPlugin.integration;

import java.util.ArrayList;
import java.util.List;

import me.azenet.UHPlugin.UHPlugin;

import org.bukkit.Bukkit;


public class UHProtocolLibIntegrationWrapper {

private UHPlugin p = null;
private UHProtocolLibIntegration integration = null;

public UHProtocolLibIntegrationWrapper(UHPlugin p) {
this.p = p;

// Needed to avoid a NoClassDefFoundError.
// I don't like this way of doing this, but else, the plugin will not load without ProtocolLib.
if(Bukkit.getServer().getPluginManager().getPlugin("ProtocolLib") != null) {
integration = new UHProtocolLibIntegration(p);
}
else {
p.getLogger().warning("ProtocolLib is not present, so the integration was disabled.");
}
}

/**
* Returns true if ProtocolLib is installed and integrated into the plugin.
* @return
*/
public boolean isProtocolLibIntegrationEnabled() {
return (this.integration != null);
}

/**
* Checks if there are some enabled option which require ProtocolLib.
*
* @return A list of enabled options which requires ProtocolLib, or null
* if there isn't any enabled option that requires ProtocolLib.
*/
public List<String> isProtocolLibNeeded() {

ArrayList<String> options = new ArrayList<String>();
options.add("hardcore-hearts");

ArrayList<String> enabledOptions = new ArrayList<String>();

for(String option : options) {
if(p.getConfig().getBoolean(option)) {
enabledOptions.add(option);
}
}

if(enabledOptions.size() != 0) {
return enabledOptions;
}
else {
return null;
}
}

/**
* Returns the wrapped integration.
*
* @return
*/
public UHProtocolLibIntegration getIntegration() {
return integration;
}
}
32 changes: 30 additions & 2 deletions src/main/java/me/azenet/UHPlugin/listeners/UHGameListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package me.azenet.UHPlugin.listeners;

import java.util.ArrayList;
import java.util.List;

import me.azenet.UHPlugin.UHPlugin;
import me.azenet.UHPlugin.UHTeam;
import me.azenet.UHPlugin.i18n.I18n;
Expand Down Expand Up @@ -70,7 +73,8 @@ public UHGameListener(UHPlugin p) {
* - save the location of the death of the player, to allow a teleportation later;
* - show the death location on the dynmap (if needed);
* - give XP to the killer (if needed);
* - broadcast the winners and launch the fireworks if needed.
* - broadcast the winners and launch the fireworks if needed;
* - notify the player about the possibility of respawn if hardcore hearts are enabled.
*
* @param ev
*/
Expand Down Expand Up @@ -180,7 +184,7 @@ public void run() {
// Shows the death location on the dynmap
p.getDynmapIntegration().showDeathLocation(ev.getEntity());

// Broadcasts the winner(s) and launches some fireworks if needed, a few seconds later.
// Broadcasts the winner(s) and launches some fireworks if needed, a few seconds later
if(p.getConfig().getBoolean("finish.auto.do")) {
Bukkit.getScheduler().runTaskLater(p, new BukkitRunnable() {
@Override
Expand All @@ -193,6 +197,16 @@ public void run() {
}
}, p.getConfig().getInt("finish.auto.timeAfterLastDeath", 3) * 20L);
}

// Notifies the player about the possibility of respawn if hardcore hearts are enabled
if(p.getConfig().getBoolean("hardcore-hearts") && p.getProtocolLibIntegrationWrapper().isProtocolLibIntegrationEnabled()) {
Bukkit.getScheduler().runTaskLater(p, new BukkitRunnable() {
@Override
public void run() {
ev.getEntity().sendMessage(i.t("death.canRespawn"));
}
}, 2L);
}
}


Expand Down Expand Up @@ -288,6 +302,20 @@ public void onPlayerJoin(final PlayerJoinEvent ev) {
ev.getPlayer().sendMessage(i.t("load.SPNotInstalled1"));
ev.getPlayer().sendMessage(i.t("load.SPNotInstalled2"));
}

// The same for ProtocolLib
if(!p.getProtocolLibIntegrationWrapper().isProtocolLibIntegrationEnabled()) {
List<String> enabledOptionsWithProtocolLibNeeded = p.getProtocolLibIntegrationWrapper().isProtocolLibNeeded();

if(enabledOptionsWithProtocolLibNeeded != null) {
ev.getPlayer().sendMessage(i.t("load.PLNotInstalled1"));
ev.getPlayer().sendMessage(i.t("load.PLNotInstalled2"));
for(String option : enabledOptionsWithProtocolLibNeeded) {
ev.getPlayer().sendMessage(i.t("load.PLNotInstalledItem", option));
}
ev.getPlayer().sendMessage(i.t("load.PLNotInstalled3", "http://dev.bukkit.org/bukkit-plugins/protocollib/"));
}
}
}

// If the player needs to be resurrected...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Plugin UltraHardcore (UHPlugin)
* Copyright (C) 2013 azenet
* Copyright (C) 2014 Amaury Carrade
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/].
*/

package me.azenet.UHPlugin.listeners;

import me.azenet.UHPlugin.UHPlugin;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;


public class UHLoginPacketsListener extends PacketAdapter {

public UHLoginPacketsListener(UHPlugin p) {
super(p, ListenerPriority.NORMAL, PacketType.Play.Server.LOGIN);
}

@Override
public void onPacketSending(PacketEvent event) {
// If its a login packet, write the hardcore flag (first boolean) to true.
if (event.getPacketType().equals(PacketType.Play.Server.LOGIN)) {
event.getPacket().getBooleans().write(0, true);
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ slow-start:
delayBetweenTP: 3 # in seconds


# Display hardcore hearts instead of normal ones?
hardcore-hearts: true


# Controls the behavior of the /uh finish command
finish:
auto:
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/i18n/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ keys:
SPNotInstalled1: "{darkred}[UHC] {ce}SpectatorPlus is not installed."
SPNotInstalled2: "{gray}If you want a spectator mode, install the plugin; UHPlugin will configure it automatically."

PLNotInstalled1: "{darkred}[UHC] {ce}ProtocolLib is needed but not installed!"
PLNotInstalled2: "{gray}The following options require the presence of ProtocolLib:"
PLNotInstalledItem: "{darkgray} - {gray}{0}"
PLNotInstalled3: "{gray}ProtocolLib is available here: {0}"

cmd:
errorLoad: "{ce}An error occurred, see console for details. This is probably a bug."
errorUnknown: "{ce}This subcommand does not exists. See /uh for the available commands."
Expand Down Expand Up @@ -128,6 +133,8 @@ keys:
kickMessage: "jayjay"
banMessage: "You are dead!"
teamHasFallen: "{0}The team {1} has fallen!"

canRespawn: "{darkpurple}{obfuscated}----{lightpurple}{italic} YOU CAN RESPAWN{lightpurple}, just click {italic}Respawn {lightpurple}on the next screen."

craft:
goldenApple:
Expand Down
Loading

0 comments on commit 28183be

Please sign in to comment.