More generic database convenience methods.

This commit is contained in:
BuildTools 2016-01-29 12:20:53 +01:00 committed by Olof Larsson
parent d80a0f4865
commit 8f6bcf2be9
2 changed files with 28 additions and 0 deletions

View File

@ -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<E extends Entity<E>>
// -------------------------------------------- //
// CONVENIENCE: DATABASE
// -------------------------------------------- //
public <T> T convertGet(T value, T defaultValue)
{
return value != null ? value : defaultValue;
}
public <T> 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;

View File

@ -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<E extends SenderEntity<E>> extends Entity<E>
// CONVENIENCE: DATABASE
// -------------------------------------------- //
public <T> 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