Better edit messages for container editor

This commit is contained in:
BuildTools 2016-02-24 16:16:11 +01:00 committed by Olof Larsson
parent e74a45bd2b
commit 7710d69383
3 changed files with 84 additions and 6 deletions

View File

@ -118,18 +118,27 @@ public class CommandEditAbstract<O, V> extends MassiveCommand
if (event.isCancelled()) return; if (event.isCancelled()) return;
after = event.getAfter(); after = event.getAfter();
// Setup
String descProperty = this.getProperty().getDisplayName();
String descObject = this.getObjectVisual();
String descValue = this.getInheritedVisual(source, before);
// NoChange // NoChange
// We check, inform and cancel on equality. // We check, inform and cancel on equality.
if (this.getValueType().equals(before, after)) if (this.getValueType().equals(before, after))
{ {
msg("%s<silver> for %s<silver> already: %s", descProperty, descObject, descValue); message(this.attemptSetNochangeMessage());
return; return;
} }
this.attemptSetPerform(after);
}
protected String attemptSetNochangeMessage()
{
return Txt.parse("%s<silver> for %s<silver> already: %s", this.getProperty().getDisplayName(), this.getObjectVisual(), this.getInheritedVisual());
}
protected void attemptSetPerform(V after)
{
String descProperty = this.getProperty().getDisplayName();
String descObject = this.getObjectVisual();
String descValue = this.getInheritedVisual();
// Create messages // Create messages
List<String> messages = new MassiveList<>(); List<String> messages = new MassiveList<>();

View File

@ -1,9 +1,11 @@
package com.massivecraft.massivecore.command.editor; package com.massivecraft.massivecore.command.editor;
import java.util.AbstractMap.SimpleImmutableEntry; import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Collection;
import java.util.List; import java.util.List;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.requirement.RequirementEditorPropertyCreated; import com.massivecraft.massivecore.command.requirement.RequirementEditorPropertyCreated;
import com.massivecraft.massivecore.command.type.Type; import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.command.type.TypeNullable; import com.massivecraft.massivecore.command.type.TypeNullable;
@ -78,6 +80,49 @@ public abstract class CommandEditContainerAbstract<O, V> extends CommandEditAbst
return alias; return alias;
} }
// -------------------------------------------- //
// ATTEMPT SET
// -------------------------------------------- //
@Override
public String attemptSetNochangeMessage()
{
return Txt.parse("%s<silver> for %s<silver> was not changed.", this.getProperty().getDisplayName(), this.getObjectVisual());
}
@SuppressWarnings("unchecked")
@Override
public void attemptSetPerform(V after)
{
V before = this.getInheritedValue();
String descProperty = this.getProperty().getDisplayName();
// Apply
// We set the new property value.
this.getProperty().setValue(this.getObject(), after);
// Create messages
List<String> messages = new MassiveList<>();
messages.add(Txt.parse("%s<silver> for %s<silver> edited:", descProperty, this.getObjectVisual()));
// Note: The result of getAdditions is not actually V, but the implementation doesn't care.
Collection<Object> additions = ContainerUtil.getAdditions(before, after);
if ( ! additions.isEmpty())
{
messages.add(Txt.parse("<k>Additions: %s", this.getValueType().getVisual((V) additions)));
}
// Note: The result of getDeletions is not actually V, but the implementation doesn't care.
Collection<Object> deletions = ContainerUtil.getDeletions(before, after);
if ( ! deletions.isEmpty())
{
messages.add(Txt.parse("<k>Deletions: %s", this.getValueType().getVisual((V) deletions)));
}
message(messages);
}
// -------------------------------------------- // // -------------------------------------------- //
// ABSTRACT // ABSTRACT
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -5,7 +5,11 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.massivecraft.massivecore.collections.MassiveSet;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.SortedSet; import java.util.SortedSet;
@ -210,4 +214,24 @@ public class ContainerUtil
} }
} }
// -------------------------------------------- //
// ADDITIONS & DELETIONS
// -------------------------------------------- //
public static <E> Collection<E> getAdditions(Object before, Object after)
{
Collection<E> elements = ContainerUtil.getElements(after);
Set<E> ret = new MassiveSet<E>(elements);
ret.removeAll(ContainerUtil.getElements(before));
return ret;
}
public static <E> Collection<E> getDeletions(Object before, Object after)
{
Collection<E> elements = ContainerUtil.getElements(before);
Set<E> ret = new MassiveSet<E>(elements);
ret.removeAll(ContainerUtil.getElements(after));
return ret;
}
} }