Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/com/github/devcyntrix/deathchest/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.devcyntrix.deathchest;

import java.text.DateFormat;

public interface Constants {

int DATE_FORMAT_CONFIG = DateFormat.DEFAULT;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.devcyntrix.deathchest.support.protection.WorldGuardDeathChestFlag;
import com.github.devcyntrix.deathchest.support.storage.MemoryStorage;
import com.github.devcyntrix.deathchest.support.storage.YamlStorage;
import com.github.devcyntrix.deathchest.util.LocationUtil;
import com.github.devcyntrix.deathchest.util.adapter.DurationAdapter;
import com.github.devcyntrix.deathchest.view.chest.*;
import com.github.devcyntrix.deathchest.view.update.AdminJoinNotificationView;
Expand Down Expand Up @@ -58,7 +59,7 @@
import java.util.logging.Level;
import java.util.stream.Stream;

import static com.github.devcyntrix.deathchest.api.report.ReportManager.DATE_FORMAT_CONFIG;
import static com.github.devcyntrix.deathchest.Constants.DATE_FORMAT_CONFIG;

/**
* This plugin creates chests if a player dies and will destroy them after a specific time.
Expand Down Expand Up @@ -200,8 +201,7 @@ public void onDisable() {
this.compatibilityManager.disableCompatibilities();
}

if (this.audiences != null)
this.audiences.close();
if (this.audiences != null) this.audiences.close();
}

@Override
Expand All @@ -222,22 +222,21 @@ public void onLoad() {
@Override
public void onEnable() {
debug(0, "Loading configuration file...");
if (!isTest())
reloadConfig();
if (!isTest()) reloadConfig();


initializeServices();
loadServices();

debug(0, "Registering commands...");
CommandRegistry.create(this).registerCommands(this);

if (!test) {
if (!isTest()) {
debug(0, "Starting metrics...");
new Metrics(this, BSTATS_ID);
}
}

private void initializeServices() {
private void loadServices() {
placeholderAPIEnabled = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
this.audiences = BukkitAudiences.create(this);

Expand Down Expand Up @@ -283,7 +282,7 @@ private void initializeServices() {
this.placeHolderController = new PlaceholderController(getDeathChestConfig());

debug(0, "Using death chest yaml storage");
if (!test) {
if (!isTest()) {
this.deathChestStorage = new YamlStorage(this.placeHolderController);
} else {
this.deathChestStorage = new MemoryStorage();
Expand Down Expand Up @@ -399,7 +398,7 @@ public void saveChests() {
public void reload() {
onDisable();
reloadConfig();
initializeServices();
loadServices();
}

@Override
Expand All @@ -411,11 +410,7 @@ public void reloadConfig() {
debug(1, "Parsing configuration file...");
this.deathChestConfig = DeathChestConfig.load(getConfig());
if (isDebugMode()) {
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.setDateFormat(DATE_FORMAT_CONFIG, DATE_FORMAT_CONFIG)
.registerTypeAdapter(Duration.class, new DurationAdapter())
.create();
Gson gson = new GsonBuilder().setPrettyPrinting().setDateFormat(DATE_FORMAT_CONFIG, DATE_FORMAT_CONFIG).registerTypeAdapter(Duration.class, new DurationAdapter()).create();
debug(1, "Configuration: " + gson.toJson(this.deathChestConfig));
}
}
Expand All @@ -428,15 +423,10 @@ public void reloadConfig() {
*/
@Override
public boolean canPlaceChestAt(@NotNull Location location) {
World world = location.getWorld();
if (world == null)
return false;
if (location.getY() < world.getMinHeight())
return false;
if (location.getY() >= world.getMaxHeight())
return false;

return deathChestController.getChest(location) == null && !location.getBlock().getType().isSolid() && location.getBlock().getType() != Material.NETHER_PORTAL;
return LocationUtil.isValidBlock(location) &&
deathChestController.getChest(location) == null &&
!location.getBlock().getType().isSolid() &&
location.getBlock().getType() != Material.NETHER_PORTAL; // To avoid a nether portal bug
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Date;
Expand All @@ -13,9 +12,6 @@

public interface ReportManager {


int DATE_FORMAT_CONFIG = DateFormat.DEFAULT;

default void createReport() {
addReport(Report.create());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public record DeathChestConfig(
@SerializedName("world-filter") @NotNull WorldFilterConfig worldFilterConfig,
@SerializedName("world-chest-protection-filter") @NotNull WorldFilterConfig worldChestProtectionFilter,
@SerializedName("world-alias") @NotNull WorldAliasConfig worldAlias,
@SerializedName("drop-conditions") @NotNull DropConditionConfig dropCondition,
@SerializedName("preferred-animation-service") @Nullable String preferredBlockBreakAnimationService) {

public static final int CONFIG_VERSION = 3;
Expand Down Expand Up @@ -57,9 +58,11 @@ public static DeathChestConfig load(FileConfiguration config) {
WorldFilterConfig worldChestProtectionFilterConfig = WorldFilterConfig.load(config.getConfigurationSection("world-chest-protection-filter"));
WorldAliasConfig worldAliasConfig = WorldAliasConfig.load(config.getConfigurationSection("world-alias"));

DropConditionConfig dropConditions = DropConditionConfig.load(config.getConfigurationSection("drop-conditions"));

String preferredAnimationService = config.getString("preferred-animation-service");

return new DeathChestConfig(configVersion, debug, updateCheck, autoUpdate, durationFormat, chestOptions, inventoryOptions, hologramOptions, particleOptions, breakAnimationOptions, playerNotificationOptions, globalNotificationOptions, changeDeathMessageOptions, worldFilterConfig, worldChestProtectionFilterConfig, worldAliasConfig, preferredAnimationService);
return new DeathChestConfig(configVersion, debug, updateCheck, autoUpdate, durationFormat, chestOptions, inventoryOptions, hologramOptions, particleOptions, breakAnimationOptions, playerNotificationOptions, globalNotificationOptions, changeDeathMessageOptions, worldFilterConfig, worldChestProtectionFilterConfig, worldAliasConfig, dropConditions, preferredAnimationService);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.devcyntrix.deathchest.config;

import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;

public interface DropCondition {

boolean shouldDropItems(@NotNull Location location);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.devcyntrix.deathchest.config;

import com.github.devcyntrix.deathchest.util.LocationUtil;
import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.Set;

public class DropConditionConfig {

@SerializedName("void-death")
private boolean voidDeath;
@SerializedName("lava-death")
private boolean lavaDeath;
@SerializedName("fire-death")
private boolean fireDeath;

@Getter
private final transient Set<DropCondition> appliedConditions = new HashSet<>();

public DropConditionConfig(boolean voidDeath, boolean lavaDeath, boolean fireDeath) {
this.voidDeath = voidDeath;
this.lavaDeath = lavaDeath;
this.fireDeath = fireDeath;

if (voidDeath) appliedConditions.add(location -> {
return location.getBlockY() < location.getWorld().getMinHeight();
});
if (lavaDeath) appliedConditions.add(location -> {
return LocationUtil.isValidBlock(location) && location.getBlock().getType() == Material.LAVA;
});
if (fireDeath) appliedConditions.add(location -> {
return LocationUtil.isValidBlock(location) && location.getBlock().getType() == Material.FIRE;
});
}

public boolean shouldDrop(Location location) {
return appliedConditions.stream().anyMatch(dropCondition -> dropCondition.shouldDropItems(location));
}

@Contract("null -> new")
public static @NotNull DropConditionConfig load(@Nullable ConfigurationSection section) {
if (section == null) section = new MemoryConfiguration();

boolean voidDeath = section.getBoolean("void-death", false);
boolean lavaDeath = section.getBoolean("lava-death", false);
boolean fireDeath = section.getBoolean("fire-death", false);

return new DropConditionConfig(voidDeath, lavaDeath, fireDeath);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.devcyntrix.deathchest.DeathChestPlugin;
import com.github.devcyntrix.deathchest.util.LastLocationMetadata;
import com.github.devcyntrix.deathchest.util.LocationUtil;
import com.google.inject.Singleton;
import org.bukkit.Location;
import org.bukkit.block.Block;
Expand All @@ -24,6 +25,10 @@ public LastSafeLocationController(DeathChestPlugin plugin) {

public void updatePosition(Player player) {
Location location = player.getLocation().clone().subtract(0, 0.2, 0);

if (!LocationUtil.isValidBlock(location))
return;

Block block = location.getBlock();
if (block.isEmpty()) // Check if the player is in air
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public void onDeath(PlayerDeathEvent event) {
if (!config.worldFilterConfig().test(player.getWorld()))
return;

if (config.dropCondition().shouldDrop(player.getLocation()))
return;

plugin.debug(1, "Getting expiration time...");
Duration expiration = config.chestOptions().expiration();
if (expiration == null)
Expand Down Expand Up @@ -160,7 +163,7 @@ public void onDeath(PlayerDeathEvent event) {
Location lastSafePos = controller.getPosition(player);

if (lastSafePos == null) {
lastSafePos = player.getLocation().getBlock().getLocation().clone();
lastSafePos = player.getLocation().clone();
} else if (player.getLocation().distanceSquared(lastSafePos) >= 20 * 20) {
// Spawn the chest near to the player death location if the safe position distance is higher than 20 Blocks
lastSafePos = player.getLocation().getBlock().getLocation().clone();
Expand Down
Loading