Small cleanup

This commit is contained in:
Indyuce 2020-10-03 20:23:47 +02:00
parent ed9c86942b
commit 68f77205dd
6 changed files with 145 additions and 95 deletions

View File

@ -169,7 +169,7 @@ public class MMOCore extends JavaPlugin {
dataProvider = new MySQLDataProvider();
if(getConfig().isConfigurationSection("default-playerdata"))
dataProvider.getDataManager().setDefaults(getConfig().getConfigurationSection("default-playerdata"));
dataProvider.getDataManager().loadDefaultData(getConfig().getConfigurationSection("default-playerdata"));
if (Bukkit.getPluginManager().getPlugin("Vault") != null)
economy = new VaultEconomy();
@ -377,7 +377,7 @@ public class MMOCore extends JavaPlugin {
skillManager.reload();
mineManager.clear();
mineManager.reload(getConfig().getBoolean("protect-custom-mine"));
mineManager.reload();
fishingManager.clear();
alchemyManager.clear();

View File

@ -36,33 +36,47 @@ public class BlockListener implements Listener {
String savedData = event.getBlock().getBlockData().getAsString();
Block block = event.getBlock();
final boolean regen = MMOCore.plugin.mineManager.isRegenerating(block);
if(regen && !MMOCore.plugin.mineManager.isBlockRegistered(block)) {
event.setCancelled(true);
return;
}
/*
* if custom mining enabled, check for item breaking restrictions
* Check for custom mining in the current region first.
*/
boolean customMine = MMOCore.plugin.mineManager.isEnabled(player, block.getLocation());
if (!customMine)
return;
BlockInfo info = MMOCore.plugin.mineManager.getInfo(block);
if (info == null) {
if(MMOCore.plugin.mineManager.shouldProtect())
event.setCancelled(true);
return;
}
if(!info.getBlock().breakRestrictions(block)) {
/*
* If the block is a temporary block, immediately cancel the break event
*/
if (MMOCore.plugin.mineManager.isTemporaryBlock(block)) {
event.setCancelled(true);
return;
}
/*
* calls the event and listen for cancel & for drops changes... also
* Check if the block has exp or drop tables
*/
BlockInfo info = MMOCore.plugin.mineManager.getInfo(block);
if (info == null) {
/*
* If players are prevented from breaking blocks in custom mining
* regions
*/
if (MMOCore.plugin.mineManager.shouldProtect())
event.setCancelled(true);
return;
}
/*
* Extra breaking conditions.
*/
if (!info.getBlock().breakRestrictions(block)) {
event.setCancelled(true);
return;
}
/*
* Calls the event and listen for cancel & for drops changes... also
* allows to apply tool durability & enchants to drops, etc.
*/
CustomBlockMineEvent called = new CustomBlockMineEvent(PlayerData.get(player), block, info);
@ -80,7 +94,7 @@ public class BlockListener implements Listener {
}
/*
* remove vanilla drops if needed
* Remove vanilla drops if needed
*/
if (!info.hasVanillaDrops()) {
// event.setDropItems(false); // May not work
@ -89,7 +103,7 @@ public class BlockListener implements Listener {
}
/*
* apply triggers, add experience info to the event so the other events
* Apply triggers, add experience info to the event so the other events
* can give exp to other TOOLS and display HOLOGRAMS
*/
if (info.hasTriggers() && !block.hasMetadata("player_placed")) {
@ -107,7 +121,7 @@ public class BlockListener implements Listener {
}
/*
* apply drop tables
* Apply drop tables
*/
if (info.hasDropTable()) {
Location dropLocation = getSafeDropLocation(block,
@ -118,10 +132,10 @@ public class BlockListener implements Listener {
}
/*
* enable block regen.
* Finally enable block regen.
*/
if (info.hasRegen())
MMOCore.plugin.mineManager.initialize(info.startRegeneration(Bukkit.createBlockData(savedData), block.getLocation()), !regen);
MMOCore.plugin.mineManager.initialize(info.startRegeneration(Bukkit.createBlockData(savedData), block.getLocation()));
}
@EventHandler(priority = EventPriority.HIGH)

View File

@ -67,7 +67,7 @@ public class CustomBlockManager extends MMOManager {
public boolean isBlockRegistered(Block block) {
return map.containsKey(findBlockType(block).generateKey());
}
public BlockInfo getInfo(Block block) {
return map.getOrDefault(findBlockType(block).generateKey(), null);
}
@ -82,41 +82,59 @@ public class CustomBlockManager extends MMOManager {
return new VanillaBlockType(block);
}
public void initialize(RegeneratingBlock info, boolean schedule) {
if(schedule) {
active.add(info);
Bukkit.getScheduler().runTaskLater(MMOCore.plugin, () -> regen(info, false), info.getRegeneratingBlock().getRegenerationInfo().getTime());
}
public void initialize(RegeneratingBlock info) {
// if (schedule) {
active.add(info);
Bukkit.getScheduler().runTaskLater(MMOCore.plugin, () -> regen(info, false), info.getRegeneratingBlock().getRegenerationInfo().getTime());
// }
if (info.getRegeneratingBlock().getRegenerationInfo().hasTemporaryBlock())
info.getRegeneratingBlock().getRegenerationInfo().getTemporaryBlock().place(info.getLocation(), info);
}
/**
* Called when a block regens, either due to regen timer or because the
* server shuts down.
*
* @param info
* Block which must be regened
* @param shutdown
* Must be set to true if the server is shutting down. When the
* server shuts down, it iterates through active blocks. This
* prevents any issue when editing lists being iterated
*/
private void regen(RegeneratingBlock info, boolean shutdown) {
Location infoLocation = info.getLocation();
Location infoLocation = info.getLocation();
// Get the chunk and load it async if needed.
PaperLib.getChunkAtAsync(infoLocation).whenComplete((chunk, ex) -> {
info.getRegeneratingBlock().getBlock().place(infoLocation, info);
info.getLocation().getBlock().getState().update();
if(!shutdown) active.remove(info);
});
PaperLib.getChunkAtAsync(infoLocation).whenComplete((chunk, ex) -> {
info.getRegeneratingBlock().getBlock().place(infoLocation, info);
info.getLocation().getBlock().getState().update();
if (!shutdown)
active.remove(info);
});
}
/*
* called when the server disables so every mined block which was in timer
/**
* Called when the server disables so every mined block which was in timer
* are reset and put back in place.
*/
public void resetRemainingBlocks() {
active.forEach(info -> regen(info, true));
}
public boolean isRegenerating(Block block) {
/**
* @param block
* Potentially vanilla block being broken by a player
* @return Returns if the block being broken is a temporary block. If it is,
* players should not be able to break it
*/
public boolean isTemporaryBlock(Block block) {
Location loc = block.getLocation();
for(RegeneratingBlock info : active)
if(info.getLocation().getBlockX() == loc.getBlockX()
&& info.getLocation().getBlockY() == loc.getBlockY()
&& info.getLocation().getBlockZ() == loc.getBlockZ())
for (RegeneratingBlock info : active)
if (info.getLocation().getBlockX() == loc.getBlockX() && info.getLocation().getBlockY() == loc.getBlockY()
&& info.getLocation().getBlockZ() == loc.getBlockZ())
return true;
return false;
}
@ -125,8 +143,9 @@ public class CustomBlockManager extends MMOManager {
}
public boolean isEnabled(Entity entity, Location loc) {
if(customMineConditions.isEmpty()) return false;
if (customMineConditions.isEmpty())
return false;
ConditionInstance conditionEntity = new ConditionInstance(entity, loc);
for (Condition condition : customMineConditions)
if (!condition.isMet(conditionEntity))
@ -143,20 +162,20 @@ public class CustomBlockManager extends MMOManager {
MMOCore.log(Level.WARNING, "Could not load custom block '" + key + "': " + exception.getMessage());
}
}
/**
* @return If block breaking should be denied in custom mining regions
*/
public boolean shouldProtect() {
return protect;
}
public void reload(boolean protect) {
this.protect = protect;
reload();
}
@Override
public void reload() {
customMineConditions.clear();
this.protect = MMOCore.plugin.getConfig().getBoolean("protect-custom-mine");
for (String key : MMOCore.plugin.getConfig().getStringList("custom-mine-conditions"))
try {
customMineConditions.add(MMOCore.plugin.loadManager.loadCondition(new MMOLineConfig(key)));

View File

@ -17,8 +17,7 @@ import net.Indyuce.mmocore.api.player.PlayerData;
import net.mmogroup.mmolib.api.player.MMOPlayerData;
public abstract class PlayerDataManager {
// private final Map<UUID, PlayerData> map = new HashMap<>();
private DefaultPlayerData defaults = new DefaultPlayerData();
private DefaultPlayerData defaultData = new DefaultPlayerData();
public PlayerData get(OfflinePlayer player) {
return get(player.getUniqueId());
@ -39,7 +38,7 @@ public abstract class PlayerDataManager {
public void setup(Player player) {
/*
* setup playerData based on loadData method to support both MySQL and
* Setup playerData based on loadData method to support both MySQL and
* YAML data storage
*/
MMOPlayerData mmoData = MMOPlayerData.get(player);
@ -47,39 +46,43 @@ public abstract class PlayerDataManager {
PlayerData generated = new PlayerData(mmoData);
/*
* loads player data and ONLY THEN refresh the player statistics and
* Loads player data and ONLY THEN refresh the player statistics and
* calls the load event on the MAIN thread
*/
Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> {
loadData(generated, defaults);
loadData(generated);
Bukkit.getScheduler().runTask(MMOCore.plugin, () -> Bukkit.getPluginManager().callEvent(new PlayerDataLoadEvent(generated)));
generated.getStats().updateStats();
});
}
}
public DefaultPlayerData getDefaultData() {
return defaultData;
}
public void loadDefaultData(ConfigurationSection config) {
defaultData = new DefaultPlayerData(config);
}
public boolean isLoaded(UUID uuid) {
return MMOPlayerData.isLoaded(uuid) && MMOPlayerData.get(uuid).getMMOCore() != null;
}
public Collection<PlayerData> getLoaded() {
return MMOPlayerData.getLoaded().stream().filter(data -> data.getMMOCore() != null).map(data -> data.getMMOCore()).collect(Collectors.toSet());
return MMOPlayerData.getLoaded().stream().filter(data -> data.getMMOCore() != null).map(data -> data.getMMOCore())
.collect(Collectors.toSet());
}
public abstract void loadData(PlayerData data, DefaultPlayerData defaults);
public abstract void loadData(PlayerData data);
public abstract void saveData(PlayerData data);
public abstract void remove(PlayerData data);
public void setDefaults(ConfigurationSection config) {
defaults = new DefaultPlayerData(config);
}
@Getter
public class DefaultPlayerData {
private final int level, classPoints, skillPoints,
attributePoints, attrReallocPoints;
private final int level, classPoints, skillPoints, attributePoints, attrReallocPoints;
public DefaultPlayerData(ConfigurationSection config) {
level = config.getInt("level", 1);
@ -96,5 +99,25 @@ public abstract class PlayerDataManager {
attributePoints = 0;
attrReallocPoints = 0;
}
public int getLevel() {
return level;
}
public int getSkillPoints() {
return skillPoints;
}
public int getClassPoints() {
return classPoints;
}
public int getAttributeReallocationPoints() {
return attrReallocPoints;
}
public int getAttributePoints() {
return attributePoints;
}
}
}

View File

@ -33,23 +33,22 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
}
@Override
public void loadData(PlayerData data, DefaultPlayerData defaults) {
ResultSet result = provider
.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';");
public void loadData(PlayerData data) {
ResultSet result = provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';");
// player data not initialized yet
if (result.size() < 1) {
data.setLevel(defaults.getLevel());
data.setClassPoints(defaults.getClassPoints());
data.setSkillPoints(defaults.getSkillPoints());
data.setAttributePoints(defaults.getAttributePoints());
data.setAttributeReallocationPoints(defaults.getAttrReallocPoints());
data.setLevel(getDefaultData().getLevel());
data.setClassPoints(getDefaultData().getClassPoints());
data.setSkillPoints(getDefaultData().getSkillPoints());
data.setAttributePoints(getDefaultData().getAttributePoints());
data.setAttributeReallocationPoints(getDefaultData().getAttributeReallocationPoints());
data.setExperience(0);
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
data.getQuestData().updateBossBar();
return;
}
@ -67,8 +66,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
if (!isEmpty(row.getString("guild")))
data.setGuild(MMOCore.plugin.dataProvider.getGuildManager().stillInGuild(data.getUniqueId(),
row.getString("guild")));
data.setGuild(MMOCore.plugin.dataProvider.getGuildManager().stillInGuild(data.getUniqueId(), row.getString("guild")));
if (!isEmpty(row.getString("attributes")))
data.getAttributes().load(row.getString("attributes"));
if (!isEmpty(row.getString("professions")))
@ -97,16 +95,14 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
Validate.notNull(profess, "Could not find class '" + entry.getKey() + "'");
data.applyClassInfo(profess, new SavedClassInformation(entry.getValue().getAsJsonObject()));
} catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING,
"Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
}
}
}
}
private boolean isEmpty(String s) {
return s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]")
|| s.equalsIgnoreCase("");
return s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]") || s.equalsIgnoreCase("");
}
@Override
@ -124,10 +120,8 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
sql.updateData("guild", data.hasGuild() ? data.getGuild().getId() : null);
sql.updateJSONArray("waypoints", data.getWaypoints());
sql.updateJSONArray("friends",
data.getFriends().stream().map(uuid -> uuid.toString()).collect(Collectors.toList()));
sql.updateJSONArray("bound_skills",
data.getBoundSkills().stream().map(skill -> skill.getSkill().getId()).collect(Collectors.toList()));
sql.updateJSONArray("friends", data.getFriends().stream().map(uuid -> uuid.toString()).collect(Collectors.toList()));
sql.updateJSONArray("bound_skills", data.getBoundSkills().stream().map(skill -> skill.getSkill().getId()).collect(Collectors.toList()));
sql.updateJSONObject("skills", data.mapSkillLevels().entrySet());
@ -194,7 +188,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
friends = new ArrayList<UUID>();
} else {
RowData row = result.get(0);
level = row.getInt("level");
lastLogin = row.getLong("last_login");
profess = isEmpty(row.getString("class")) ? MMOCore.plugin.classManager.getDefaultClass()

View File

@ -21,14 +21,14 @@ import net.Indyuce.mmocore.manager.data.PlayerDataManager;
public class YAMLPlayerDataManager extends PlayerDataManager {
@Override
public void loadData(PlayerData data, DefaultPlayerData defaults) {
public void loadData(PlayerData data) {
FileConfiguration config = new ConfigFile(data.getPlayer()).getConfig();
data.setClassPoints(config.getInt("class-points", defaults.getClassPoints()));
data.setSkillPoints(config.getInt("skill-points", defaults.getSkillPoints()));
data.setAttributePoints(config.getInt("attribute-points", defaults.getAttributePoints()));
data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points", defaults.getAttrReallocPoints()));
data.setLevel(config.getInt("level", defaults.getLevel()));
data.setClassPoints(config.getInt("class-points", getDefaultData().getClassPoints()));
data.setSkillPoints(config.getInt("skill-points", getDefaultData().getSkillPoints()));
data.setAttributePoints(config.getInt("attribute-points", getDefaultData().getAttributePoints()));
data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points", getDefaultData().getAttributeReallocationPoints()));
data.setLevel(config.getInt("level", getDefaultData().getLevel()));
data.setExperience(config.getInt("experience"));
if (config.contains("class"))
data.setClass(MMOCore.plugin.classManager.get(config.getString("class")));