From 8f6bcf2be9b61c4e47cbdec27555a0cd9fda090f Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 29 Jan 2016 12:20:53 +0100 Subject: [PATCH] More generic database convenience methods. --- src/com/massivecraft/massivecore/store/Entity.java | 14 ++++++++++++++ .../massivecore/store/SenderEntity.java | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/com/massivecraft/massivecore/store/Entity.java b/src/com/massivecraft/massivecore/store/Entity.java index 6fd9fd98..baf7f7e5 100644 --- a/src/com/massivecraft/massivecore/store/Entity.java +++ b/src/com/massivecraft/massivecore/store/Entity.java @@ -1,5 +1,7 @@ package com.massivecraft.massivecore.store; +import java.util.Objects; + import com.massivecraft.massivecore.MassiveCore; import com.massivecraft.massivecore.store.accessor.Accessor; import com.massivecraft.massivecore.xlib.gson.Gson; @@ -171,7 +173,19 @@ public class Entity> // -------------------------------------------- // // CONVENIENCE: DATABASE // -------------------------------------------- // + + public T convertGet(T value, T defaultValue) + { + return value != null ? value : defaultValue; + } + public T convertSet(T value, T defaultValue) + { + this.changed(); + return Objects.equals(value, defaultValue) ? null : value; + } + + // Boolean specific public boolean convertGet(Boolean wrapper) { return wrapper != null ? wrapper.booleanValue() : false; diff --git a/src/com/massivecraft/massivecore/store/SenderEntity.java b/src/com/massivecraft/massivecore/store/SenderEntity.java index e97dfc6e..0b2455af 100644 --- a/src/com/massivecraft/massivecore/store/SenderEntity.java +++ b/src/com/massivecraft/massivecore/store/SenderEntity.java @@ -8,6 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; +import com.google.common.base.Objects; import com.massivecraft.massivecore.Named; import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.mson.Mson; @@ -64,6 +65,19 @@ public abstract class SenderEntity> extends Entity // CONVENIENCE: DATABASE // -------------------------------------------- // + public T convertGet(T value, T defaultValue, String permission) + { + // Create + T ret = super.convertGet(value, defaultValue); + + // Permission Requirement + if ( ! Objects.equal(value, defaultValue) && ! PermUtil.has(this.getSender(), permission)) return defaultValue; + + // Return + return ret; + } + + // Boolean specific public boolean convertGet(Boolean wrapper, String permission) { // Create