Merge EditSettings with Delegate by adding parent.

This commit is contained in:
Olof Larsson 2016-05-26 11:18:25 +02:00
parent 5c26c3a503
commit eee611bfd6
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
3 changed files with 80 additions and 46 deletions

View File

@ -8,21 +8,21 @@ public class CommandEditProperties<O, V> extends CommandEditAbstract<O, V>
// CONSTRUCT
// -------------------------------------------- //
public CommandEditProperties(EditSettings<O> settings, Property<O, V> property)
public CommandEditProperties(EditSettings<O> parentSettings, Property<O, V> childProperty)
{
// Super
super(settings, property, null);
super(parentSettings, childProperty, null);
this.addChild(new CommandEditShow<>(settings, property));
this.addChild(new CommandEditShow<>(parentSettings, childProperty));
// Parameters
if (property.isEditable())
if (childProperty.isEditable())
{
Type<V> type = this.getValueType();
EditSettings<V> fieldSettings = new EditSettingsDelegate<>(settings, property);
EditSettings<V> childSettings = new EditSettings<>(parentSettings, childProperty);
for (Property<V, ?> prop : type.getInnerProperties())
{
this.addChild(prop.createEditCommand(fieldSettings));
this.addChild(prop.createEditCommand(childSettings));
}
}
}

View File

@ -17,6 +17,43 @@ import com.massivecraft.massivecore.util.Txt;
public class EditSettings<O>
{
// -------------------------------------------- //
// PARENTS
// -------------------------------------------- //
private EditSettings<?> parent = null;
public EditSettings<?> getParent() { return this.parent; }
public void setParent(EditSettings<?> parent) { this.parent = parent; }
public boolean hasParent() { return this.getParent() != null; }
public List<EditSettings<?>> getParents()
{
// Create
List<EditSettings<?>> ret = new MassiveList<>();
// Fill
EditSettings<?> parent = this;
while (parent.hasParent())
{
parent = parent.getParent();
ret.add(parent);
}
// Return
return ret;
}
public boolean isRoot() { return ! this.hasParent(); }
public EditSettings<?> getRoot()
{
EditSettings<?> ret = this;
while (ret.hasParent())
{
ret = ret.getParent();
}
return ret;
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
@ -60,13 +97,13 @@ public class EditSettings<O>
public void addPropertyRequirements(Requirement... requirements) { this.addPropertyRequirements(Arrays.asList(requirements)); }
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// CONSTRUCT > NORMAL
// -------------------------------------------- //
public EditSettings(Type<O> objectType, Property<CommandSender, O> objectProperty)
public EditSettings(Type<O> objectType, Property<CommandSender, O> usedProperty)
{
this.objectType = objectType;
this.usedProperty = objectProperty;
this.usedProperty = usedProperty;
}
public EditSettings(Type<O> objectType)
@ -74,6 +111,40 @@ public class EditSettings<O>
this(objectType, null);
}
// -------------------------------------------- //
// CONSTRUCT > DELEGATE
// -------------------------------------------- //
public <P> EditSettings(final EditSettings<P> parentSettings, final Property<P, O> childProperty)
{
this(childProperty.getValueType());
this.setParent(parentSettings);
PropertyUsed<O> usedProperty = new PropertyUsed<O>(this) {
@Override
public O getRaw(CommandSender sender)
{
P parentUsed = parentSettings.getUsed(sender);
return childProperty.getRaw(parentUsed);
}
@Override
public CommandSender setRaw(CommandSender sender, O used)
{
P parentUsed = parentSettings.getUsed(sender);
childProperty.setRaw(parentUsed, used);
return sender;
}
};
this.setUsedProperty(usedProperty);
this.addUsedRequirements(parentSettings.getPropertyRequirements());
this.addUsedRequirements(childProperty.getRequirements());
}
// -------------------------------------------- //
// OBJECT
// -------------------------------------------- //

View File

@ -1,37 +0,0 @@
package com.massivecraft.massivecore.command.editor;
import org.bukkit.command.CommandSender;
public class EditSettingsDelegate<O, V> extends EditSettings<V>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public EditSettingsDelegate(final EditSettings<O> outerSettings, final Property<O, V> property)
{
super(property.getValueType());
PropertyUsed<V> usedProperty = new PropertyUsed<V>(this) {
@Override
public V getRaw(CommandSender sender)
{
return property.getRaw(outerSettings.getUsed(sender));
}
@Override
public CommandSender setRaw(CommandSender sender, V used)
{
property.setRaw(outerSettings.getUsed(sender), used);
return sender;
}
};
this.setUsedProperty(usedProperty);
this.addUsedRequirements(outerSettings.getPropertyRequirements());
this.addUsedRequirements(property.getRequirements());
}
}