Skip to content

Commit 23fa881

Browse files
committed
add CorpseBuilder api
1 parent e4a8710 commit 23fa881

6 files changed

Lines changed: 129 additions & 27 deletions

File tree

src/main/java/com/github/unldenis/corpse/api/CorpseAPI.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.github.unldenis.corpse.corpse.*;
2222
import com.github.unldenis.corpse.manager.*;
23-
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
2423
import org.apache.commons.lang.*;
2524
import org.bukkit.*;
2625
import org.bukkit.entity.*;
@@ -58,7 +57,7 @@ public static synchronized CorpseAPI getInstance() {
5857
*/
5958
public Corpse spawnCorpse(@NotNull Player player) {
6059
Validate.notNull(player, "Player cannot be null");
61-
return new Corpse(player);
60+
return Corpse.fromPlayer(player).spawn();
6261
}
6362

6463
/**
@@ -71,7 +70,7 @@ public Corpse spawnCorpse(@NotNull Player player) {
7170
public Corpse spawnCorpse(@NotNull Player player, @NotNull Location location) {
7271
Validate.notNull(player, "Player cannot be null");
7372
Validate.notNull(location, "Spawn location cannot be null");
74-
return new Corpse(location, player, null);
73+
return Corpse.fromPlayer(player).location(location).spawn();
7574
}
7675

7776
/**
@@ -84,7 +83,7 @@ public Corpse spawnCorpse(@NotNull Player player, @NotNull Location location) {
8483
public Corpse spawnCorpse(@NotNull OfflinePlayer offlinePlayer, @NotNull Location location) {
8584
Validate.notNull(offlinePlayer, "OfflinePlayer cannot be null");
8685
Validate.notNull(location, "Spawn location cannot be null");
87-
return new Corpse(location, offlinePlayer, null);
86+
return Corpse.fromLocation(location).name(offlinePlayer.getName()).spawn();
8887
}
8988

9089
/**
@@ -108,8 +107,7 @@ public Corpse spawnCorpse(
108107
) {
109108
Validate.notNull(player, "Player cannot be null");
110109
Validate.notNull(location, "Spawn location cannot be null");
111-
return new Corpse(location, SpigotReflectionUtil.getUserProfile(player),
112-
new ItemStack[]{boots, leggings, chestPlate, helmet}, player.getName());
110+
return Corpse.fromPlayer(player).location(location).armorContents(new ItemStack[]{boots, leggings, chestPlate, helmet}).spawn();
113111
}
114112

115113
/**
@@ -133,8 +131,7 @@ public Corpse spawnCorpse(
133131
) {
134132
Validate.notNull(offlinePlayer, "OfflinePlayer cannot be null");
135133
Validate.notNull(location, "Spawn location cannot be null");
136-
return new Corpse(location, offlinePlayer,
137-
new ItemStack[]{boots, leggings, chestPlate, helmet});
134+
return Corpse.fromLocation(location).name(offlinePlayer.getName()).armorContents(new ItemStack[]{boots, leggings, chestPlate, helmet}).spawn();
138135
}
139136

140137
/**

src/main/java/com/github/unldenis/corpse/command/RemoveCorpseCommand.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.bukkit.entity.*;
2626
import org.jetbrains.annotations.*;
2727

28-
import java.util.concurrent.atomic.*;
2928

3029
public class RemoveCorpseCommand implements CommandExecutor {
3130

src/main/java/com/github/unldenis/corpse/command/SpawnCorpseCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd,
3535
Player player = (Player) sender;
3636
if (player.hasPermission("corpses.spawn")) {
3737
if (args.length == 0) {
38-
new Corpse(player);
38+
Corpse.fromPlayer(player).spawn();
3939
player.sendMessage(ChatColor.GREEN + "Corpse created");
4040
return true;
4141
} else if (args.length == 1) {
4242
OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]);
4343
if (target.isOnline()) {
44-
CorpseAPI.getInstance().spawnCorpse((Player) target, player.getLocation());
44+
Corpse.fromPlayer((Player) target).location(player.getLocation()).spawn();
4545
} else {
46-
new Corpse(player.getLocation(), target, null);
46+
Corpse.fromLocation(player.getLocation()).name(target.getName()).spawn();
4747
}
4848
player.sendMessage(ChatColor.GREEN + "Corpse created");
4949
return true;

src/main/java/com/github/unldenis/corpse/corpse/Corpse.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,31 @@
4848
import java.util.*;
4949
import java.util.concurrent.CopyOnWriteArraySet;
5050

51+
/**
52+
* Corpse class that represents a dead body.
53+
* To create a Corpse, use fromPlayer, fromLocation methods.
54+
*/
5155
public class Corpse {
5256

57+
58+
/**
59+
* Create a CorpseBuilder from a player.
60+
* @param player The player to create the CorpseBuilder for.
61+
* @return A CorpseBuilder object.
62+
*/
63+
public static CorpseBuilder fromPlayer(@NotNull Player player) {
64+
return new CorpseBuilder(player);
65+
}
66+
67+
/**
68+
* Create a CorpseBuilder from a location.
69+
* @param location The location to create the CorpseBuilder for.
70+
* @return A CorpseBuilder object.
71+
*/
72+
public static CorpseBuilder fromLocation(@NotNull Location location) {
73+
return new CorpseBuilder(location);
74+
}
75+
5376
protected final int id;
5477
protected final Location location;
5578
protected final UserProfile profile;
@@ -59,8 +82,7 @@ public class Corpse {
5982
private final CorpseNPC internalNPC;
6083
private final boolean hasArmor;
6184

62-
@ApiStatus.Internal
63-
public Corpse(
85+
Corpse(
6486
@NotNull Location location,
6587
@NotNull List<TextureProperty> textures,
6688
@Nullable ItemStack[] armorContents,
@@ -104,18 +126,6 @@ public Corpse(
104126

105127
}
106128

107-
public Corpse(@NotNull Player player) {
108-
this(player.getLocation(), SpigotReflectionUtil.getUserProfile(player), player.getInventory().getArmorContents(), player.getName());
109-
}
110-
111-
public Corpse(
112-
@NotNull Location location,
113-
@NotNull OfflinePlayer offlinePlayer,
114-
@Nullable ItemStack[] armorContents
115-
) {
116-
this(location, new ArrayList<>(), armorContents, offlinePlayer.getName());
117-
}
118-
119129
@ApiStatus.Internal
120130
public void show(@NotNull Player player) {
121131
this.seeingPlayers.add(player);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.github.unldenis.corpse.corpse;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.entity.Player;
5+
6+
import com.github.retrooper.packetevents.protocol.player.TextureProperty;
7+
8+
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
import org.bukkit.inventory.ItemStack;
14+
import org.jetbrains.annotations.NotNull;
15+
16+
/**
17+
* Builder for creating a Corpse object.
18+
*/
19+
public class CorpseBuilder {
20+
21+
22+
private Location location;
23+
private List<TextureProperty> textures = new ArrayList<>();
24+
private ItemStack[] armorContents = null;
25+
private String name = null;
26+
27+
/**
28+
* Constructor for creating a CorpseBuilder with a player.
29+
* @param player The player to create the CorpseBuilder for.
30+
*/
31+
CorpseBuilder(@NotNull Player player) {
32+
this.location = player.getLocation();
33+
this.textures = SpigotReflectionUtil.getUserProfile(player);
34+
this.armorContents = player.getInventory().getArmorContents();
35+
this.name = player.getName();
36+
}
37+
38+
/**
39+
* Constructor for creating a CorpseBuilder with a location.
40+
* @param location The location to create the CorpseBuilder for.
41+
*/
42+
CorpseBuilder(@NotNull Location location) {
43+
this.location = location;
44+
}
45+
46+
/**
47+
* Set the location of the corpse.
48+
* @param location The location to set.
49+
* @return The CorpseBuilder object.
50+
*/
51+
public CorpseBuilder location(@NotNull Location location) {
52+
this.location = location;
53+
return this;
54+
}
55+
56+
/**
57+
* Set the textures of the corpse.
58+
* @param textures The textures to set.
59+
* @return The CorpseBuilder object.
60+
*/
61+
public CorpseBuilder textures(@NotNull List<TextureProperty> textures) {
62+
this.textures = textures;
63+
return this;
64+
}
65+
66+
/**
67+
* Set the armor contents of the corpse.
68+
* @param armorContents The armor contents to set.
69+
* @return The CorpseBuilder object.
70+
*/
71+
public CorpseBuilder armorContents(@NotNull ItemStack[] armorContents) {
72+
this.armorContents = armorContents;
73+
return this;
74+
}
75+
76+
/**
77+
* Set the name of the corpse.
78+
* @param name The name to set.
79+
* @return The CorpseBuilder object.
80+
*/
81+
public CorpseBuilder name(@NotNull String name) {
82+
this.name = name;
83+
return this;
84+
}
85+
86+
87+
/**
88+
* Spawn the corpse.
89+
* @return A Corpse object.
90+
*/
91+
public Corpse spawn() {
92+
return new Corpse(location, textures, armorContents, name);
93+
}
94+
95+
96+
}

src/main/java/com/github/unldenis/corpse/manager/CorpsePool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public void handleDeath(PlayerDeathEvent event) {
218218
new LootableCorpse(player.getLocation(), player, drops);
219219

220220
} else {
221-
new Corpse(player);
221+
Corpse.fromPlayer(player).spawn();
222222
}
223223
}
224224

0 commit comments

Comments
 (0)