Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
MEFRREEX authored Dec 3, 2022
0 parents commit 226d8d0
Showing 6 changed files with 185 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>theoni.hpbar</groupId>
<artifactId>hpbar</artifactId>
<version>1.0</version>

<name>HealthBossBar</name>

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

<repositories>
<repository>
<id>nukkitx-repo</id>
<url>https://repo.nukkitx.com/snapshot/</url>
</repository>
</repositories>

<distributionManagement>
<repository>
<id>releases</id>
<name>nukkitx-releases</name>
<url>https://repo.nukkitx.com/release</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>nukkitx-snapshots</name>
<url>https://repo.nukkitx.com/snapshot</url>
</snapshotRepository>
</distributionManagement>

<dependencies>
<dependency>
<groupId>cn.nukkit</groupId>
<artifactId>nukkit</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<defaultGoal>clean package</defaultGoal>
<finalName>${project.name}</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources/</directory>
</resource>
</resources>
</build>
</project>
35 changes: 35 additions & 0 deletions src/main/java/theoni/hpbar/BossBarManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package theoni.hpbar;

import java.util.HashMap;
import java.util.UUID;

import cn.nukkit.Player;
import cn.nukkit.utils.DummyBossBar;

public class BossBarManager {

private static HashMap<UUID, Long> bossBars = new HashMap<UUID, Long>();

public static void addBossBar(Player player, DummyBossBar bossBar) {
bossBars.put(player.getUniqueId(), bossBar.getBossBarId());
}

public static void removeBossBar(Player player) {
if (playerHasBossBar(player)) {
DummyBossBar bossBar = getPlayerBossBar(player);
bossBar.destroy();
bossBars.remove(player.getUniqueId());
}
}

public static boolean playerHasBossBar(Player player) {
Long id = bossBars.get(player.getUniqueId());
return id != null;
}

public static DummyBossBar getPlayerBossBar(Player player) {
Long id = bossBars.get(player.getUniqueId());
return player.getDummyBossBar(id);
}

}
13 changes: 13 additions & 0 deletions src/main/java/theoni/hpbar/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package theoni.hpbar;

import cn.nukkit.event.Listener;
import cn.nukkit.plugin.PluginBase;
import theoni.hpbar.listener.*;

public class Main extends PluginBase implements Listener {

public void onEnable() {
saveDefaultConfig();
getServer().getPluginManager().registerEvents((Listener)new EventListener(this), (Main)this);
}
}
66 changes: 66 additions & 0 deletions src/main/java/theoni/hpbar/listener/EventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package theoni.hpbar.listener;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

import cn.nukkit.Player;
import cn.nukkit.entity.Entity;
import cn.nukkit.event.EventHandler;
import cn.nukkit.event.Listener;
import cn.nukkit.event.entity.EntityDamageByEntityEvent;
import cn.nukkit.utils.Config;
import cn.nukkit.utils.DummyBossBar;
import cn.nukkit.utils.BossBarColor;
import cn.nukkit.utils.DummyBossBar.Builder;
import theoni.hpbar.Main;

import theoni.hpbar.BossBarManager;

public class EventListener implements Listener {

Main plugin;
Config config;
private HashMap<UUID, Long> lastHit = new HashMap<UUID, Long>();

public EventListener(Main plugin) {
this.plugin = plugin;
this.config = plugin.getConfig();
}

@EventHandler
public void onDamage(EntityDamageByEntityEvent event) {
Entity entity = event.getEntity();
Player damager = (Player) event.getDamager();
if (!(event.getDamager() instanceof Player)) return;

Float health = entity.getHealth();
Float maxhealth = (float) entity.getMaxHealth();

float length = health * ((health % maxhealth) / 4);
DummyBossBar bar = new Builder(damager)
.text(config.getString("bossbar-text")
.replace("{name}", entity.getName())
.replace("{tag}", entity.getNameTag())
.replace("{health}", health.toString())
.replace("{maxhealth}", maxhealth.toString()))
.color(BossBarColor.valueOf(config.getString("color")))
.length(length)
.build();
damager.createBossBar(bar);
BossBarManager.removeBossBar(damager);
BossBarManager.addBossBar(damager, bar);
lastHit.put(damager.getUniqueId(), System.currentTimeMillis());

// Удаление бара через 5 секунд после удара по игроку
(new Timer()).schedule(new TimerTask() {
public void run() {
if (System.currentTimeMillis() - lastHit.get(damager.getUniqueId()) >= config.getInt("bossbar-remove-time") * 1000) {
BossBarManager.removeBossBar(damager);
}
}
}, config.getInt("bossbar-remove-time") * 1000);
}
}

8 changes: 8 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Доступные заполнители: {name}, {tag}, {health}, {maxhealth}.
bossbar-text: "{name}"

# Доступные цвета: PINK, BLUE, RED, GREEN, YELLOW, PURPLE, WHITE.
color: "RED"

# Время через которое будет пропадать бар. В секундах.
bossbar-remove-time: 5
6 changes: 6 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: ${project.name}
version: ${project.version}
api: ["1.0.8"]
author: The Oni
description: ${project.description}
main: theoni.hpbar.Main

0 comments on commit 226d8d0

Please sign in to comment.