Skip to content

Commit

Permalink
Fixed minor bugs. Release 0.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
PauMAVA committed Dec 23, 2019
1 parent cb1bcda commit 8682c11
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/me/PauMAVA/UhcPlugin/UhcPluginCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import me.PauMAVA.UhcPlugin.commands.UhcCompleteTab;
import me.PauMAVA.UhcPlugin.gameplay.CustomRecipes;
import me.PauMAVA.UhcPlugin.match.UhcMatchHandler;
import me.PauMAVA.UhcPlugin.match.UhcScoreboardManager;
import me.PauMAVA.UhcPlugin.teams.UhcTeamsManager;
import me.PauMAVA.UhcPlugin.util.EventsRegister;
import me.PauMAVA.UhcPlugin.util.PacketIntercepter;
import me.PauMAVA.UhcPlugin.util.UhcTabList;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down Expand Up @@ -58,6 +60,8 @@ public void onEnable() {
public void onDisable() {
for(Player p: Bukkit.getServer().getOnlinePlayers()) {
PacketIntercepter.rmPlayer(p);
UhcScoreboardManager.rmPlayer(p);
UhcTabList.resetTab(p);
}
UhcLogger.info("Disabled UhcPlugin!");
}
Expand Down
5 changes: 1 addition & 4 deletions src/me/PauMAVA/UhcPlugin/commands/UhcConfigCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void config(CommandSender theSender, String[] args) {
String value = args[2];
switch(option) {
case "season": {
setInt(option, value);
setString(option, value);
break;
}
case "chapter_length": {
Expand Down Expand Up @@ -171,9 +171,6 @@ public static Difficulty getDifficultyObject() {
case "normal": {
return Difficulty.NORMAL;
}
case "hard": {
return Difficulty.HARD;
}
default: {
return Difficulty.HARD;
}
Expand Down
4 changes: 4 additions & 0 deletions src/me/PauMAVA/UhcPlugin/match/UhcMatchHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import me.PauMAVA.UhcPlugin.teams.UhcTeam;
import me.PauMAVA.UhcPlugin.teams.UhcTeamsManager;
import me.PauMAVA.UhcPlugin.util.PacketIntercepter;
import me.PauMAVA.UhcPlugin.util.UhcTabList;
import me.PauMAVA.UhcPlugin.world.UhcWorldConfig;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand All @@ -39,6 +40,7 @@ public class UhcMatchHandler {
private UhcPluginCore plugin;
private Boolean isRunning = false;
private Integer timerTaskID;
private Integer tabTaskID;
private List<Player> matchPlayers = new ArrayList<>();
private List<UhcTeam> teams = new ArrayList<UhcTeam>();
private UhcMatchTimer timer;
Expand All @@ -65,11 +67,13 @@ public void start() {
}
this.timer = new UhcMatchTimer();
this.timerTaskID = timer.runTaskTimer(plugin, 0L, 20L).getTaskId();
this.tabTaskID = new UhcTabList().runTaskTimer(plugin, 0L, 20L).getTaskId();
}

public void end() {
this.isRunning = false;
Bukkit.getScheduler().cancelTask(this.timerTaskID);
Bukkit.getScheduler().cancelTask(this.tabTaskID);
}

/* Adds a player to the match
Expand Down
20 changes: 13 additions & 7 deletions src/me/PauMAVA/UhcPlugin/match/UhcScoreboardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public class UhcScoreboardManager {
private static int chapterLength = plugin.getConfig().getInt("chapter_length");

public static void setUp() {
infoObjective = uhcScoreboard.registerNewObjective("Info", "dummy", ChatColor.GOLD + "" + ChatColor.BOLD + "UHC S" + plugin.getConfig().get("season"));
lifeObjective = uhcScoreboard.registerNewObjective("Health", "health", "health", RenderType.HEARTS);
lifeObjective.setDisplaySlot(DisplaySlot.PLAYER_LIST);
updateHealth();
for(Player player: Bukkit.getOnlinePlayers()) {
player.setScoreboard(uhcScoreboard);
}
uhcScoreboard = scManager.getNewScoreboard();
infoObjective = uhcScoreboard.registerNewObjective("Info", "dummy", ChatColor.GOLD + "" + ChatColor.BOLD + "UHC S" + plugin.getConfig().get("season"));
lifeObjective = uhcScoreboard.registerNewObjective("Health", "health", "health", RenderType.HEARTS);
lifeObjective.setDisplaySlot(DisplaySlot.PLAYER_LIST);
updateHealth();
for(Player player: Bukkit.getOnlinePlayers()) {
player.setScoreboard(uhcScoreboard);
}
}

/* Updates the health on the list scoreboard */
Expand Down Expand Up @@ -97,4 +98,9 @@ static void refreshSidebar(String minutes, String seconds, String episode, Strin
static void refreshSidebarTitle(String newTitle) {
infoObjective.setDisplayName(newTitle);
}

public static void rmPlayer(Player player) {
player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
}

}
4 changes: 1 addition & 3 deletions src/me/PauMAVA/UhcPlugin/teams/UhcTeamsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,4 @@ public void run() {
}
}.runTaskLater(plugin, 40L);
}


}
}
13 changes: 8 additions & 5 deletions src/me/PauMAVA/UhcPlugin/util/EventsRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.advancement.Advancement;
import org.bukkit.event.player.*;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.entity.Drowned;
import org.bukkit.entity.Player;
Expand All @@ -38,10 +39,6 @@
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

Expand Down Expand Up @@ -121,7 +118,13 @@ public void onEntityKill(EntityDeathEvent event) {
item.setItemMeta((ItemMeta) meta);
event.getEntity().getWorld().dropItem(event.getEntity().getLocation(), item);
}
Bukkit.getServer().broadcastMessage("KILLED Drowned!: " + randomNum);
}
}

@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
if(plugin.getMatchHandler().getMatchStatus() && event.getCause().equals(PlayerTeleportEvent.TeleportCause.SPECTATE)) {
event.setCancelled(true);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/me/PauMAVA/UhcPlugin/util/PacketIntercepter.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ public void write(ChannelHandlerContext context, Object packet, ChannelPromise p
};

ChannelPipeline pipe = ((CraftPlayer)player).getHandle().playerConnection.networkManager.channel.pipeline();
pipe.addBefore("packet_handler", player.getName(), channelDuplexHandler);
try{
pipe.addBefore("packet_handler", player.getName(), channelDuplexHandler);
} catch (IllegalArgumentException ignored) {}
}

}
109 changes: 109 additions & 0 deletions src/me/PauMAVA/UhcPlugin/util/UhcTabList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* UhcPlugin
* Copyright (c) 2019 Pau Machetti Vallverdu
*
* 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.PauMAVA.UhcPlugin.util;

import me.PauMAVA.UhcPlugin.UhcPluginCore;
import me.PauMAVA.UhcPlugin.teams.UhcTeam;
import net.minecraft.server.v1_15_R1.ChatComponentText;
import net.minecraft.server.v1_15_R1.PacketPlayOutPlayerListHeaderFooter;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.lang.reflect.Field;

public class UhcTabList extends BukkitRunnable {

private ChatComponentText headerComp;
private ChatComponentText footerComp;
private String season = UhcPluginCore.getInstance().getConfig().getString("season");
private int i = 1;

@Override
public void run() {
switch (i) {
case 1: {
this.headerComp = new ChatComponentText("\n" + ChatColor.YELLOW + "" + ChatColor.BOLD + "U" + ChatColor.GOLD + "" + ChatColor.BOLD + "HC S" + season + "\n");
break;
}
case 2: {
this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "U" + ChatColor.YELLOW + "" + ChatColor.BOLD + "H" + ChatColor.GOLD + "" + ChatColor.BOLD + "C S" + season + "\n");
break;
}
case 3: {
this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UH" + ChatColor.YELLOW + "" + ChatColor.BOLD + "C" + ChatColor.GOLD + "" + ChatColor.BOLD + " S" + season + "\n");
break;
}
case 4: {
this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UHC" + ChatColor.YELLOW + "" + ChatColor.BOLD + " " + ChatColor.GOLD + "" + ChatColor.BOLD + "S" + season + "\n");
break;
}
case 5: {
this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UHC " + ChatColor.YELLOW + "" + ChatColor.BOLD + "S" + ChatColor.GOLD + "" + ChatColor.BOLD + "" + season + "\n");
break;
}
case 6: {
this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UHC S" + ChatColor.YELLOW + "" + ChatColor.BOLD + season + "\n");
break;
}
case 7: {
this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UHC S" + season + "\n");
i = -20;
break;
}
}
i++;
Integer alivePlayers = 0;
for(UhcTeam team: UhcPluginCore.getInstance().getMatchHandler().getRemainingTeams()) {
alivePlayers += team.alive().size();
}
this.footerComp = new ChatComponentText("\n" + ChatColor.AQUA + "" + ChatColor.BOLD + "Alive Players: " + ChatColor.RESET + "" + alivePlayers + "\n" + ChatColor.LIGHT_PURPLE + "\n(c) 2019 UhcPlugin by PauMAVA\n");
PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
try {
Field headerField = packet.getClass().getDeclaredField("header");
Field footerField = packet.getClass().getDeclaredField("footer");
headerField.setAccessible(true);
footerField.setAccessible(true);
headerField.set(packet, this.headerComp);
footerField.set(packet, this.footerComp);
for(Player player: Bukkit.getServer().getOnlinePlayers()) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}

public static void resetTab(Player player) {
PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
try{
Field header = packet.getClass().getDeclaredField("header");
Field footer = packet.getClass().getDeclaredField("footer");
header.setAccessible(true);
footer.setAccessible(true);
header.set(packet, new ChatComponentText(""));
footer.set(packet, new ChatComponentText(""));
} catch (IllegalAccessException | NoSuchFieldException | IllegalArgumentException e) {
e.printStackTrace();
}
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
}
}

0 comments on commit 8682c11

Please sign in to comment.