mirror of
https://github.com/CarmJos/cn2b2t-project.git
synced 2026-06-04 08:48:16 +08:00
代码重构
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>cn2b2t</artifactId>
|
||||
<groupId>org.cn2b2t</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>2b2t-death</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot</artifactId>
|
||||
<version>1.12.2-R0.1-SNAPSHOT</version>
|
||||
<systemPath>${basedir}/../_lib/spigot.jar</systemPath>
|
||||
<scope>system</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.cn2b2t</groupId>
|
||||
<artifactId>2b2t-core</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.cn2b2t</groupId>
|
||||
<artifactId>2b2t-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.cn2b2t.functions.death;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.cn2b2t.core.managers.utils.UserManager;
|
||||
import org.cn2b2t.functions.death.commands.Suicide;
|
||||
import org.cn2b2t.functions.death.listeners.BedListener;
|
||||
import org.cn2b2t.functions.death.listeners.DeathListener;
|
||||
import org.cn2b2t.functions.death.listeners.RespawnListener;
|
||||
import org.cn2b2t.functions.death.managers.users.UserBedManager;
|
||||
|
||||
|
||||
public class Main extends JavaPlugin {
|
||||
|
||||
public static String pluginName = "2b2t-common";
|
||||
public static String pluginVersion = "1.0-SNAPSHOT";
|
||||
|
||||
private static Main instance;
|
||||
|
||||
public static Main getInstance() {
|
||||
return Main.instance;
|
||||
}
|
||||
|
||||
private void pluginMessage(String s) {
|
||||
this.getServer().getConsoleSender().sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "!" + ChatColor.DARK_GRAY + "] " + ChatColor.AQUA + pluginName + " " + ChatColor.WHITE + pluginVersion + ChatColor.GRAY + s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
Main.instance = this;
|
||||
this.getCommand("suicide").setExecutor(new Suicide());
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new RespawnListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new DeathListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new BedListener(), this);
|
||||
|
||||
UserManager.regHandler(UserBedManager.class);
|
||||
|
||||
pluginMessage("已启用。");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
UserManager.removeHandler(UserBedManager.class);
|
||||
this.pluginMessage("已卸载。");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.cn2b2t.functions.death.commands;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.cn2b2t.common.Main;
|
||||
|
||||
public class Suicide implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (cmd.getName().equalsIgnoreCase("suicide")) {
|
||||
if (sender instanceof Player) ((Player) sender).setHealth(0);
|
||||
sender.sendMessage(Main.color(
|
||||
"&f这个世界虽然不完美,但我们仍然可以治愈自己。\n" +
|
||||
"&7全国免费心理咨询热线 &a&l800-810-1117"));
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.cn2b2t.functions.death.listeners;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.cn2b2t.common.functions.ProfileData;
|
||||
import org.cn2b2t.functions.death.managers.users.UserBedManager;
|
||||
|
||||
public class BedListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onBed(PlayerInteractEvent e) {
|
||||
if (e.getPlayer().getWorld().getName().equalsIgnoreCase("world")
|
||||
&& e.getAction() == Action.RIGHT_CLICK_BLOCK
|
||||
&& e.getClickedBlock() != null
|
||||
&& (e.getClickedBlock().getType() == Material.BED || e.getClickedBlock().getType() == Material.BED_BLOCK)) {
|
||||
UserBedManager.get(e.getPlayer()).saveBedLocation(e.getClickedBlock().getLocation());
|
||||
e.getPlayer().setBedSpawnLocation(e.getClickedBlock().getLocation(), true);
|
||||
e.getPlayer().sendMessage("§7已为您设置出生点。注意,若床被破坏,您将失去无法再次与此处重生!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.cn2b2t.functions.death.listeners;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.SkullType;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class DeathListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onDeath(PlayerDeathEvent e) {
|
||||
if (e.getEntity().getKiller() != null && new Random().nextDouble() < 0.02) {
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore.add(e.getDeathMessage());
|
||||
ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
|
||||
SkullMeta meta = (SkullMeta) skull.getItemMeta();
|
||||
meta.setOwner(e.getEntity().getName());
|
||||
meta.setLore(lore);
|
||||
skull.setItemMeta(meta);
|
||||
e.getDrops().add(skull);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.cn2b2t.functions.death.listeners;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
import org.cn2b2t.functions.death.managers.users.UserBedManager;
|
||||
import org.cn2b2t.functions.death.managers.utils.DeathManager;
|
||||
|
||||
public class RespawnListener implements Listener {
|
||||
|
||||
|
||||
@EventHandler
|
||||
public void onDeath(PlayerRespawnEvent e) {
|
||||
Player p = e.getPlayer();
|
||||
p.setMaxHealth(20);
|
||||
p.setHealth(20);
|
||||
p.setFoodLevel(30);
|
||||
|
||||
if (UserBedManager.get(p).isBedThere()) {
|
||||
e.setRespawnLocation(UserBedManager.get(p).getBedLocation());
|
||||
} else {
|
||||
p.sendMessage("§7由于您之前的床已被破坏或遮挡,无法将您传送到您的床边。");
|
||||
e.setRespawnLocation(DeathManager.randomLocation(Bukkit.getWorld("world")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDeath(PlayerChangedWorldEvent e) {
|
||||
Player p = e.getPlayer();
|
||||
if (e.getFrom().getName().equalsIgnoreCase("world_the_end")) {
|
||||
if (UserBedManager.get(p).isBedThere()) {
|
||||
p.teleport(UserBedManager.get(p).getBedLocation());
|
||||
} else {
|
||||
p.sendMessage("§7由于您之前的床已被破坏或遮挡,无法将您传送到您的床边。");
|
||||
p.teleport(DeathManager.randomLocation(Bukkit.getWorld("world")));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package org.cn2b2t.functions.death.managers.users;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.cn2b2t.core.Main;
|
||||
import org.cn2b2t.core.managers.utils.UserManager;
|
||||
import org.cn2b2t.core.modules.users.AbstractUserHandler;
|
||||
import org.cn2b2t.core.modules.users.User;
|
||||
|
||||
public class UserBedManager extends AbstractUserHandler {
|
||||
|
||||
Player p;
|
||||
User u;
|
||||
|
||||
public Location bedLocation;
|
||||
public Location spawnLocation;
|
||||
|
||||
public static UserBedManager get(User u) {
|
||||
return u.containsHandler(UserBedManager.class) ? u.getHandler(UserBedManager.class) : null;
|
||||
}
|
||||
|
||||
public static UserBedManager get(Player p) {
|
||||
return get(UserManager.getUser(p));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
p = getUser().getPlayer();
|
||||
u = getUser();
|
||||
|
||||
bedLocation = new Location(Bukkit.getWorld("world"),
|
||||
getUser().getDatas().getDouble("bedLocation.x", 0D),
|
||||
getUser().getDatas().getDouble("bedLocation.y", 101D),
|
||||
getUser().getDatas().getDouble("bedLocation.z", 0D),
|
||||
getUser().getDatas().getLong("bedLocation.yaw", 0L),
|
||||
getUser().getDatas().getLong("bedLocation.pitch", 0L));
|
||||
|
||||
spawnLocation = new Location(Bukkit.getWorld("world"),
|
||||
getUser().getDatas().getDouble("spawnLocation.x", 0D),
|
||||
getUser().getDatas().getDouble("spawnLocation.y", 101D),
|
||||
getUser().getDatas().getDouble("spawnLocation.z", 0D),
|
||||
getUser().getDatas().getLong("spawnLocation.yaw", 0L),
|
||||
getUser().getDatas().getLong("spawnLocation.pitch", 0L));
|
||||
|
||||
callLoadedEvent(Main.getInstance());
|
||||
}
|
||||
|
||||
public Location getBedLocation() {
|
||||
return bedLocation;
|
||||
}
|
||||
|
||||
public Location getSpawnLocation() {
|
||||
return spawnLocation;
|
||||
}
|
||||
|
||||
public void saveBedLocation(Location bedlocation) {
|
||||
this.bedLocation = bedlocation;
|
||||
this.spawnLocation = p.getLocation();
|
||||
getDatas().set("spawnLocation.x", p.getLocation().getX());
|
||||
getDatas().set("spawnLocation.y", p.getLocation().getY());
|
||||
getDatas().set("spawnLocation.z", p.getLocation().getZ());
|
||||
getDatas().set("spawnLocation.yaw", p.getLocation().getYaw());
|
||||
getDatas().set("spawnLocation.pitch", p.getLocation().getPitch());
|
||||
getDatas().set("bedLocation.x", bedlocation.getX());
|
||||
getDatas().set("bedLocation.y", bedlocation.getY());
|
||||
getDatas().set("bedLocation.z", bedlocation.getZ());
|
||||
getDatas().set("bedLocation.pitch", bedlocation.getPitch());
|
||||
getUser().saveDatas();
|
||||
}
|
||||
|
||||
public boolean isBedThere() {
|
||||
return getBedLocation().getBlock().getType() == Material.BED_BLOCK
|
||||
&& (bedLocation.getX() < 0 ? -bedLocation.getX() : bedLocation.getX()) <= (getBedLocation().getWorld().getWorldBorder().getSize() * 0.5)
|
||||
&& (bedLocation.getZ() < 0 ? -bedLocation.getZ() : bedLocation.getZ()) <= (getBedLocation().getWorld().getWorldBorder().getSize() * 0.5);
|
||||
}
|
||||
|
||||
private FileConfiguration getDatas() {
|
||||
return getUser().getDatas();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.cn2b2t.functions.death.managers.utils;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Biome;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class DeathManager {
|
||||
|
||||
|
||||
public static Location randomLocation(World w) {
|
||||
Random r = new Random();
|
||||
|
||||
while (true) {
|
||||
int x = -15000 + r.nextInt(30001);
|
||||
int z = -15000 + r.nextInt(30001);
|
||||
int y = w.getHighestBlockYAt(x, z);
|
||||
Location loc = new Location(w, x, y, z);
|
||||
Biome locBiome = w.getBiome(loc.getChunk().getX(), loc.getChunk().getZ());
|
||||
if (locBiome != Biome.OCEAN
|
||||
&& locBiome != Biome.DEEP_OCEAN
|
||||
&& locBiome != Biome.RIVER
|
||||
&& locBiome != Biome.DESERT
|
||||
&& locBiome != Biome.DESERT_HILLS
|
||||
&& !canReplace(loc.getBlock().getType())) {
|
||||
return loc.add(0, 1, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean canReplace(Material material) {
|
||||
switch (material) {
|
||||
case WATER:
|
||||
case LAVA:
|
||||
case STATIONARY_WATER:
|
||||
case STATIONARY_LAVA:
|
||||
case AIR:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
name: 2b2t-death
|
||||
main: org.cn2b2t.functions.death.Main
|
||||
version: 1.0.0 - SNAPSHOT
|
||||
authors:
|
||||
- Moci
|
||||
- cn2b2t
|
||||
depend:
|
||||
- 2b2t-core
|
||||
- 2b2t-common
|
||||
commands:
|
||||
suicide:
|
||||
aliases:
|
||||
- kill
|
||||
- killme
|
||||
Reference in New Issue
Block a user