2.0h TypePattern and better editor requirements

This commit is contained in:
BuildTools 2016-03-30 21:19:44 +02:00 committed by Olof Larsson
parent 5b6b29f6a9
commit 422ceed9ad
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
14 changed files with 359 additions and 44 deletions

View File

@ -715,7 +715,8 @@ public class MassiveCommand implements Active, PluginIdentifiableCommand
public List<Requirement> getRequirements() { return this.requirements; } public List<Requirement> getRequirements() { return this.requirements; }
public void setRequirements(List<Requirement> requirements) { this.requirements = requirements; } public void setRequirements(List<Requirement> requirements) { this.requirements = requirements; }
public void addRequirements(Requirement... requirements) { this.requirements.addAll(Arrays.asList(requirements)); } public void addRequirements(Collection<Requirement> requirements) { this.requirements.addAll(requirements); }
public void addRequirements(Requirement... requirements) { this.addRequirements(Arrays.asList(requirements)); }
public boolean isRequirementsMet(CommandSender sender, boolean verboose) public boolean isRequirementsMet(CommandSender sender, boolean verboose)
{ {

View File

@ -5,17 +5,14 @@ import java.util.Map.Entry;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.MassiveCommand; import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.command.requirement.RequirementEditorUse; import com.massivecraft.massivecore.command.requirement.RequirementEditorUse;
import com.massivecraft.massivecore.command.requirement.RequirementHasPerm;
import com.massivecraft.massivecore.command.type.Type; import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.event.EventMassiveCoreEditorEdit; import com.massivecraft.massivecore.event.EventMassiveCoreEditorEdit;
import com.massivecraft.massivecore.mson.Mson; import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.PermUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public class CommandEditAbstract<O, V> extends MassiveCommand public class CommandEditAbstract<O, V> extends MassiveCommand
@ -54,12 +51,6 @@ public class CommandEditAbstract<O, V> extends MassiveCommand
// Requirements // Requirements
this.addRequirements(RequirementEditorUse.get()); this.addRequirements(RequirementEditorUse.get());
if (this.isWrite() != null && this.isWrite())
{
Permission permission = this.getPropertyPermission();
if (permission != null) this.addRequirements(RequirementHasPerm.get(permission.getName()));
}
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -106,9 +97,6 @@ public class CommandEditAbstract<O, V> extends MassiveCommand
return; return;
} }
// Permission
if ( ! PermUtil.has(sender, this.getPropertyPermission(), true)) return;
// Inherited / Source / Before // Inherited / Source / Before
Entry<O, V> inherited = this.getInheritedEntry(); Entry<O, V> inherited = this.getInheritedEntry();
O source = inherited.getKey(); O source = inherited.getKey();
@ -202,16 +190,6 @@ public class CommandEditAbstract<O, V> extends MassiveCommand
return this.getSettings().getUsed(sender); return this.getSettings().getUsed(sender);
} }
public Permission getPropertyPermission()
{
return this.getSettings().getPropertyPermission(this.getProperty());
}
public Permission getUsedPermission()
{
return this.getSettings().getUsedPermission();
}
public Mson getObjectVisual() public Mson getObjectVisual()
{ {
return this.getObjectType().getVisualMson(this.getObject(), sender); return this.getObjectType().getVisualMson(this.getObject(), sender);

View File

@ -2,7 +2,6 @@ package com.massivecraft.massivecore.command.editor;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.TypeNullable; import com.massivecraft.massivecore.command.type.TypeNullable;
import com.massivecraft.massivecore.util.PermUtil;
public class CommandEditSimple<O, V> extends CommandEditAbstract<O, V> public class CommandEditSimple<O, V> extends CommandEditAbstract<O, V>
{ {
@ -36,9 +35,6 @@ public class CommandEditSimple<O, V> extends CommandEditAbstract<O, V>
return; return;
} }
// Permission
if ( ! PermUtil.has(sender, this.getPropertyPermission(), true)) return;
// Arguments // Arguments
V after = this.readArg(); V after = this.readArg();

View File

@ -1,11 +1,13 @@
package com.massivecraft.massivecore.command.editor; package com.massivecraft.massivecore.command.editor;
import java.util.Collections;
import java.util.List;
import java.util.Set; import java.util.Set;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.requirement.Requirement;
import com.massivecraft.massivecore.command.type.Type; import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.command.type.sender.TypeSender; import com.massivecraft.massivecore.command.type.sender.TypeSender;
import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.MUtil;
@ -38,14 +40,7 @@ public class EditSettings<O>
} }
protected EditSettings<CommandSender> createUsedSettings() protected EditSettings<CommandSender> createUsedSettings()
{ {
final EditSettings<O> main = this; return new EditSettings<CommandSender>(TypeSender.get(), new PropertyThis<CommandSender>(TypeSender.get()));
return new EditSettings<CommandSender>(TypeSender.get(), new PropertyThis<CommandSender>(TypeSender.get())) {
@Override
public Permission getPropertyPermission(Property<CommandSender,?> property)
{
return main.getUsedPermission();
}
};
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -90,14 +85,9 @@ public class EditSettings<O>
// PERMISSONS // PERMISSONS
// -------------------------------------------- // // -------------------------------------------- //
public Permission getPropertyPermission(Property<O, ?> property) public List<Requirement> getUsedRequirements()
{ {
return null; return Collections.emptyList();
}
public Permission getUsedPermission()
{
return null;
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -1,6 +1,7 @@
package com.massivecraft.massivecore.command.editor; package com.massivecraft.massivecore.command.editor;
import java.util.AbstractMap.SimpleEntry; import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -11,6 +12,8 @@ import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.Named; import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.requirement.Requirement;
import com.massivecraft.massivecore.command.requirement.RequirementAbstract;
import com.massivecraft.massivecore.command.type.Type; import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.mson.Mson; import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.store.Entity; import com.massivecraft.massivecore.store.Entity;
@ -57,6 +60,17 @@ public abstract class Property<O, V> implements Named
public void setName(String name) { this.names = new MassiveList<String>(name); } public void setName(String name) { this.names = new MassiveList<String>(name); }
public void setNames(String... names) { this.names = new MassiveList<String>(names); } public void setNames(String... names) { this.names = new MassiveList<String>(names); }
// -------------------------------------------- //
// REQUIREMENTS
// -------------------------------------------- //
protected List<Requirement> requirements = new ArrayList<Requirement>();
public List<Requirement> getRequirements() { return this.requirements; }
public void setRequirements(List<Requirement> requirements) { this.requirements = requirements; }
public void addRequirements(Collection<Requirement> requirements) { this.requirements.addAll(requirements); }
public void addRequirements(Requirement... requirements) { this.addRequirements(Arrays.asList(requirements)); }
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
@ -147,7 +161,9 @@ public abstract class Property<O, V> implements Named
public CommandEditAbstract<O, V> createEditCommand(EditSettings<O> settings) public CommandEditAbstract<O, V> createEditCommand(EditSettings<O> settings)
{ {
return this.getValueType().createEditCommand(settings, this); CommandEditAbstract<O, V> ret = this.getValueType().createEditCommand(settings, this);
ret.addRequirements(this.getRequirements());
return ret;
} }
public Mson getInheritedVisual(O object, O source, V value, CommandSender sender) public Mson getInheritedVisual(O object, O source, V value, CommandSender sender)

View File

@ -13,6 +13,7 @@ public abstract class PropertyUsed<V> extends Property<CommandSender, V>
public PropertyUsed(EditSettings<V> settings) public PropertyUsed(EditSettings<V> settings)
{ {
super(TypeSender.get(), settings.getObjectType(), "used " + settings.getObjectType().getName()); super(TypeSender.get(), settings.getObjectType(), "used " + settings.getObjectType().getName());
this.addRequirements(settings.getUsedRequirements());
} }
} }

View File

@ -0,0 +1,57 @@
package com.massivecraft.massivecore.command.requirement;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.MassiveCommand;
import com.massivecraft.massivecore.command.type.TypeItemStack;
public class RequirementHasItemInHand extends RequirementAbstract
{
private static final long serialVersionUID = 1L;
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static RequirementHasItemInHand i = new RequirementHasItemInHand(TypeItemStack.get());
public static RequirementHasItemInHand get() { return i; }
public static RequirementHasItemInHand get(TypeItemStack innerType) { return new RequirementHasItemInHand(innerType); }
public static RequirementHasItemInHand get(Material... materialWhitelist) { return get(TypeItemStack.get(materialWhitelist)); }
public RequirementHasItemInHand(TypeItemStack innerType) { this.innerType = innerType; }
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final TypeItemStack innerType;
public TypeItemStack getInnerType() { return this.innerType; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(CommandSender sender, MassiveCommand command)
{
return this.getInnerType().isValid(null, sender);
}
@Override
public String createErrorMessage(CommandSender sender, MassiveCommand command)
{
try
{
this.getInnerType().read(sender);
}
catch (MassiveException e)
{
return e.getMessages().toPlain(true);
}
return null;
}
}

View File

@ -0,0 +1,76 @@
package com.massivecraft.massivecore.command.type;
import java.util.Collections;
import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.enchantments.Enchantment;
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.util.Txt;
public class TypeEnchantment extends TypeAbstractChoice<Enchantment>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypeEnchantment i = new TypeEnchantment();
public static TypeEnchantment get() { return i; }
public TypeEnchantment()
{
this.setVisualColor(ChatColor.AQUA);
this.setAll(Enchantment.values());
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String getNameInner(Enchantment enchantment)
{
return Txt.getNicedEnumString(enchantment.getName());
}
@Override
public Set<String> getNamesInner(Enchantment enchantment)
{
Set<String> ret = new MassiveSet<>();
ret.add(this.getName(enchantment));
if (Enchantment.DAMAGE_ALL.equals(enchantment)) Collections.addAll(ret, "Sharpness");
if (Enchantment.DAMAGE_ARTHROPODS.equals(enchantment)) Collections.addAll(ret, "BaneOfArthropdos");
if (Enchantment.DAMAGE_UNDEAD.equals(enchantment)) Collections.addAll(ret, "Smite");
if (Enchantment.DIG_SPEED.equals(enchantment)) Collections.addAll(ret, "Efficiency");
if (Enchantment.DURABILITY.equals(enchantment)) Collections.addAll(ret, "Unbreaking");
if (Enchantment.THORNS.equals(enchantment)) Collections.addAll(ret);
if (Enchantment.FIRE_ASPECT.equals(enchantment)) Collections.addAll(ret);
if (Enchantment.KNOCKBACK.equals(enchantment)) Collections.addAll(ret);
if (Enchantment.LOOT_BONUS_BLOCKS.equals(enchantment)) Collections.addAll(ret, "Fortune");
if (Enchantment.LOOT_BONUS_MOBS.equals(enchantment)) Collections.addAll(ret, "Looting");
if (Enchantment.OXYGEN.equals(enchantment)) Collections.addAll(ret, "Respiration", "Breathing");
if (Enchantment.PROTECTION_ENVIRONMENTAL.equals(enchantment)) Collections.addAll(ret, "Protection");
if (Enchantment.PROTECTION_EXPLOSIONS.equals(enchantment)) Collections.addAll(ret, "BlastProtection", "ExplosionProtection");
if (Enchantment.PROTECTION_FALL.equals(enchantment)) Collections.addAll(ret, "FeatherFalling", "FallProtection");
if (Enchantment.PROTECTION_FIRE.equals(enchantment)) Collections.addAll(ret, "FireProtection");
if (Enchantment.PROTECTION_PROJECTILE.equals(enchantment)) Collections.addAll(ret, "ProectileProtection");
if (Enchantment.SILK_TOUCH.equals(enchantment)) Collections.addAll(ret);
if (Enchantment.WATER_WORKER.equals(enchantment)) Collections.addAll(ret, "AquaAffinity");
if (Enchantment.ARROW_FIRE.equals(enchantment)) Collections.addAll(ret, "Flame");
if (Enchantment.ARROW_DAMAGE.equals(enchantment)) Collections.addAll(ret, "Power");
if (Enchantment.ARROW_KNOCKBACK.equals(enchantment)) Collections.addAll(ret, "Punch");
if (Enchantment.ARROW_INFINITE.equals(enchantment)) Collections.addAll(ret, "Infinity");
if (Enchantment.LUCK.equals(enchantment)) Collections.addAll(ret, "LuckOfSea", "LuckOfTheSea");
if (Enchantment.LURE.equals(enchantment)) Collections.addAll(ret);
return ret;
}
@Override
public String getIdInner(Enchantment enchantment)
{
return enchantment.getName();
}
}

View File

@ -0,0 +1,66 @@
package com.massivecraft.massivecore.command.type.combined;
import java.util.List;
import org.bukkit.DyeColor;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.type.enumeration.TypeDyeColor;
import com.massivecraft.massivecore.command.type.enumeration.TypePatternType;
public class TypePattern extends TypeCombined<Pattern>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypePattern i = new TypePattern();
public static TypePattern get() { return i; }
public TypePattern()
{
super(
TypeDyeColor.get(),
TypePatternType.get()
);
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public List<Object> split(Pattern value)
{
return new MassiveList<Object>(
value.getColor(),
value.getPattern()
);
}
@Override
public Pattern combine(List<Object> parts)
{
DyeColor color = null;
PatternType pattern = null;
for (int i = 0 ; i < parts.size() ; i++)
{
Object part = parts.get(i);
if (i == 0)
{
color = (DyeColor)part;
}
else if (i == 1)
{
pattern = (PatternType) part;
}
}
return new Pattern(color, pattern);
}
}

View File

@ -13,6 +13,11 @@ public class TypeMap<K, V> extends TypeContainer<Map<K, V>, Entry<K, V>>
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public static <K, V> TypeMap<K, V> get(Type<K> keyType, Type<V> valueType)
{
return get(TypeEntry.get(keyType, valueType));
}
public static <K, V> TypeMap<K, V> get(TypeEntry<K, V> entryType) public static <K, V> TypeMap<K, V> get(TypeEntry<K, V> entryType)
{ {
return new TypeMap<K, V>(entryType); return new TypeMap<K, V>(entryType);

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.command.type.enumeration;
import org.bukkit.inventory.ItemFlag;
public class TypeItemFlag extends TypeEnum<ItemFlag>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypeItemFlag i = new TypeItemFlag();
public static TypeItemFlag get() { return i; }
public TypeItemFlag()
{
super(ItemFlag.class);
}
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.massivecore.command.type.enumeration;
import org.bukkit.block.banner.PatternType;
public class TypePatternType extends TypeEnum<PatternType>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypePatternType i = new TypePatternType();
public static TypePatternType get() { return i; }
public TypePatternType()
{
super(PatternType.class);
}
}

View File

@ -0,0 +1,30 @@
package com.massivecraft.massivecore.command.type.primitive;
import org.bukkit.command.CommandSender;
public class TypeShort extends TypeAbstractNumber<Short>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypeShort i = new TypeShort();
public static TypeShort get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String getName()
{
return "number";
}
@Override
public Short valueOf(String arg, CommandSender sender) throws Exception
{
return Short.parseShort(arg);
}
}

View File

@ -0,0 +1,63 @@
package com.massivecraft.massivecore.command.type.sender;
import com.massivecraft.massivecore.SenderPresence;
import com.massivecraft.massivecore.SenderType;
import com.massivecraft.massivecore.store.SenderIdSource;
import com.massivecraft.massivecore.store.SenderIdSourceMixinAllSenderIds;
import com.massivecraft.massivecore.util.IdUtil;
public class TypeSenderName extends TypeSenderIdAbstract<String>
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
private TypeSenderName(SenderIdSource source, SenderPresence presence, SenderType type)
{
super(source, presence, type);
}
private TypeSenderName(SenderIdSource source, SenderPresence presence)
{
super(source, presence);
}
private TypeSenderName(SenderIdSource source, SenderType type)
{
super(source, type);
}
private TypeSenderName(SenderIdSource source)
{
super(source);
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static final TypeSenderName i = new TypeSenderName(SenderIdSourceMixinAllSenderIds.get());
public static TypeSenderName get() { return i; }
// -------------------------------------------- //
// GET
// -------------------------------------------- //
public static TypeSenderName get(SenderIdSource source, SenderPresence presence, SenderType type) { return new TypeSenderName(source, presence, type); }
public static TypeSenderName get(SenderIdSource source, SenderPresence presence) { return new TypeSenderName(source, presence); }
public static TypeSenderName get(SenderIdSource source, SenderType type) { return new TypeSenderName(source, type); }
public static TypeSenderName get(SenderIdSource source) { return new TypeSenderName(source); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String getResultForSenderId(String senderId)
{
return IdUtil.getName(senderId);
}
}