use of com.fastasyncworldedit.core.limit.PropertyRemap in project FastAsyncWorldEdit by IntellectualSites.
the class Settings method getLimit.
public FaweLimit getLimit(Actor actor) {
FaweLimit limit;
if (actor.hasPermission("fawe.limit.*") || actor.hasPermission("fawe.bypass")) {
limit = FaweLimit.MAX.copy();
} else {
limit = new FaweLimit();
}
ArrayList<String> keys = new ArrayList<>(LIMITS.getSections());
if (keys.remove("default")) {
keys.add("default");
}
boolean limitFound = false;
for (String key : keys) {
if (actor.hasPermission("fawe.limit." + key) || !limitFound && key.equals("default")) {
limitFound = true;
LIMITS newLimit = LIMITS.get(key);
limit.MAX_ACTIONS = Math.max(limit.MAX_ACTIONS, newLimit.MAX_ACTIONS != -1 ? newLimit.MAX_ACTIONS : Integer.MAX_VALUE);
limit.MAX_CHANGES = Math.max(limit.MAX_CHANGES, newLimit.MAX_CHANGES != -1 ? newLimit.MAX_CHANGES : Long.MAX_VALUE);
limit.MAX_BLOCKSTATES = Math.max(limit.MAX_BLOCKSTATES, newLimit.MAX_BLOCKSTATES != -1 ? newLimit.MAX_BLOCKSTATES : Integer.MAX_VALUE);
limit.MAX_CHECKS = Math.max(limit.MAX_CHECKS, newLimit.MAX_CHECKS != -1 ? newLimit.MAX_CHECKS : Long.MAX_VALUE);
limit.MAX_ENTITIES = Math.max(limit.MAX_ENTITIES, newLimit.MAX_ENTITIES != -1 ? newLimit.MAX_ENTITIES : Integer.MAX_VALUE);
limit.MAX_FAILS = Math.max(limit.MAX_FAILS, newLimit.MAX_FAILS != -1 ? newLimit.MAX_FAILS : Integer.MAX_VALUE);
limit.MAX_ITERATIONS = Math.max(limit.MAX_ITERATIONS, newLimit.MAX_ITERATIONS != -1 ? newLimit.MAX_ITERATIONS : Integer.MAX_VALUE);
limit.MAX_HISTORY = Math.max(limit.MAX_HISTORY, newLimit.MAX_HISTORY_MB != -1 ? newLimit.MAX_HISTORY_MB : Integer.MAX_VALUE);
limit.MAX_EXPRESSION_MS = Math.max(limit.MAX_EXPRESSION_MS, newLimit.MAX_EXPRESSION_MS != -1 ? newLimit.MAX_EXPRESSION_MS : Integer.MAX_VALUE);
limit.INVENTORY_MODE = Math.min(limit.INVENTORY_MODE, newLimit.INVENTORY_MODE);
limit.SPEED_REDUCTION = Math.min(limit.SPEED_REDUCTION, newLimit.SPEED_REDUCTION);
limit.FAST_PLACEMENT |= newLimit.FAST_PLACEMENT;
limit.CONFIRM_LARGE &= newLimit.CONFIRM_LARGE;
limit.RESTRICT_HISTORY_TO_REGIONS &= newLimit.RESTRICT_HISTORY_TO_REGIONS;
if (limit.STRIP_NBT == null) {
limit.STRIP_NBT = newLimit.STRIP_NBT.isEmpty() ? Collections.emptySet() : new HashSet<>(newLimit.STRIP_NBT);
} else if (limit.STRIP_NBT.isEmpty() || newLimit.STRIP_NBT.isEmpty()) {
limit.STRIP_NBT = Collections.emptySet();
} else {
limit.STRIP_NBT = new HashSet<>(limit.STRIP_NBT);
limit.STRIP_NBT.retainAll(newLimit.STRIP_NBT);
if (limit.STRIP_NBT.isEmpty()) {
limit.STRIP_NBT = Collections.emptySet();
}
}
limit.UNIVERSAL_DISALLOWED_BLOCKS &= newLimit.UNIVERSAL_DISALLOWED_BLOCKS;
if (limit.DISALLOWED_BLOCKS == null) {
limit.DISALLOWED_BLOCKS = newLimit.DISALLOWED_BLOCKS.isEmpty() ? Collections.emptySet() : new HashSet<>(newLimit.DISALLOWED_BLOCKS);
} else if (limit.DISALLOWED_BLOCKS.isEmpty() || newLimit.DISALLOWED_BLOCKS.isEmpty()) {
limit.DISALLOWED_BLOCKS = Collections.emptySet();
} else {
limit.DISALLOWED_BLOCKS = new HashSet<>(limit.DISALLOWED_BLOCKS);
limit.DISALLOWED_BLOCKS.retainAll(newLimit.DISALLOWED_BLOCKS.stream().map(s -> s.contains(":") ? s.toLowerCase(Locale.ROOT) : ("minecraft:" + s).toLowerCase(Locale.ROOT)).collect(Collectors.toSet()));
if (limit.DISALLOWED_BLOCKS.isEmpty()) {
limit.DISALLOWED_BLOCKS = Collections.emptySet();
}
}
if (limit.REMAP_PROPERTIES == null) {
limit.REMAP_PROPERTIES = newLimit.REMAP_PROPERTIES.isEmpty() ? Collections.emptySet() : newLimit.REMAP_PROPERTIES.stream().flatMap(s -> {
String propertyStr = s.substring(0, s.indexOf('['));
List<Property<?>> properties = BlockTypesCache.getAllProperties().get(propertyStr.toLowerCase(Locale.ROOT));
if (properties == null || properties.isEmpty()) {
return Stream.empty();
}
String[] mappings = s.substring(s.indexOf('[') + 1, s.indexOf(']')).split(",");
Set<PropertyRemap<?>> remaps = new HashSet<>();
for (Property<?> property : properties) {
for (String mapping : mappings) {
try {
String[] fromTo = mapping.split(":");
remaps.add(property.getRemap(property.getValueFor(fromTo[0]), property.getValueFor(fromTo[1])));
} catch (IllegalArgumentException ignored) {
// This property is unlikely to be the one being targeted.
break;
}
}
}
return remaps.stream();
}).collect(Collectors.toSet());
} else if (limit.REMAP_PROPERTIES.isEmpty() || newLimit.REMAP_PROPERTIES.isEmpty()) {
limit.REMAP_PROPERTIES = Collections.emptySet();
} else {
limit.REMAP_PROPERTIES = new HashSet<>(limit.REMAP_PROPERTIES);
limit.REMAP_PROPERTIES.retainAll(newLimit.REMAP_PROPERTIES.stream().flatMap(s -> {
String propertyStr = s.substring(0, s.indexOf('['));
List<Property<?>> properties = BlockTypesCache.getAllProperties().get(propertyStr.toLowerCase(Locale.ROOT));
if (properties == null || properties.isEmpty()) {
return Stream.empty();
}
String[] mappings = s.substring(s.indexOf('[') + 1, s.indexOf(']')).split(",");
Set<PropertyRemap<?>> remaps = new HashSet<>();
for (Property<?> property : properties) {
for (String mapping : mappings) {
try {
String[] fromTo = mapping.split(":");
remaps.add(property.getRemap(property.getValueFor(fromTo[0]), property.getValueFor(fromTo[1])));
} catch (IllegalArgumentException ignored) {
// This property is unlikely to be the one being targeted.
break;
}
}
}
return remaps.stream();
}).collect(Collectors.toSet()));
if (limit.REMAP_PROPERTIES.isEmpty()) {
limit.REMAP_PROPERTIES = Collections.emptySet();
}
}
}
}
return limit;
}
use of com.fastasyncworldedit.core.limit.PropertyRemap in project FastAsyncWorldEdit by IntellectualSites.
the class DefaultBlockParser method parseProperties.
// FAWE start - make public
public static Map<Property<?>, Object> parseProperties(// FAWE end
BlockType type, String[] stateProperties, ParserContext context, // FAWE start - if null should be returned instead of throwing an error
boolean nullNotError) throws // FAWE end
NoMatchException {
Map<Property<?>, Object> blockStates = new HashMap<>();
// FAWE start - disallowed states
if (context != null && context.getActor() != null && !context.getActor().getLimit().isUnlimited()) {
for (String input : context.getActor().getLimit().DISALLOWED_BLOCKS) {
if (input.indexOf('[') == -1 && input.indexOf(']') == -1) {
continue;
}
if (!type.getId().equalsIgnoreCase(input.substring(0, input.indexOf('[')))) {
continue;
}
String[] properties = input.substring(input.indexOf('[') + 1, input.indexOf(']')).split(",");
Set<String> blocked = Arrays.stream(properties).filter(s -> {
for (String in : stateProperties) {
if (in.equalsIgnoreCase(s)) {
return true;
}
}
return false;
}).collect(Collectors.toSet());
if (!blocked.isEmpty()) {
throw new DisallowedUsageException(Caption.of("fawe.error.limit.disallowed-block", TextComponent.of(input)));
}
}
}
if (stateProperties.length > 0) {
// Parse the block data (optional)
for (String parseableData : stateProperties) {
try {
String[] parts = parseableData.split("=");
if (parts.length != 2) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new InputParseException(Caption.of("worldedit.error.parser.bad-state-format", TextComponent.of(parseableData)));
}
@SuppressWarnings("unchecked") Property<Object> propertyKey = (Property<Object>) type.getPropertyMap().get(parts[0]);
if (propertyKey == null) {
// FAWE start - nullable context
if (context != null && context.getActor() != null) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new NoMatchException(Caption.of("worldedit.error.parser.unknown-property", TextComponent.of(parts[0]), TextComponent.of(type.getId())));
} else {
WorldEdit.logger.debug("Unknown property " + parts[0] + " for block " + type.getId());
}
return Maps.newHashMap();
}
if (blockStates.containsKey(propertyKey)) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new InputParseException(Caption.of("worldedit.error.parser.duplicate-property", TextComponent.of(parts[0])));
}
Object value;
try {
value = propertyKey.getValueFor(parts[1]);
} catch (IllegalArgumentException e) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new NoMatchException(Caption.of("worldedit.error.parser.unknown-value", TextComponent.of(parts[1]), TextComponent.of(propertyKey.getName())));
}
// FAWE start - blocked states
if (context != null && context.getActor() != null && !context.getActor().getLimit().isUnlimited()) {
if (context.getActor().getLimit().REMAP_PROPERTIES != null && !context.getActor().getLimit().REMAP_PROPERTIES.isEmpty()) {
for (PropertyRemap remap : context.getActor().getLimit().REMAP_PROPERTIES) {
Object newValue = remap.apply(type, value);
if (newValue != value) {
value = newValue;
break;
}
}
}
}
// FAWE end
blockStates.put(propertyKey, value);
} catch (NoMatchException | DisallowedUsageException e) {
// Pass-through
throw e;
} catch (Exception e) {
// FAWE start - if null should be returned instead of throwing an error
if (nullNotError) {
return null;
}
// FAWE end
throw new InputParseException(Caption.of("worldedit.error.parser.bad-state-format", TextComponent.of(parseableData)));
}
}
}
return blockStates;
}
use of com.fastasyncworldedit.core.limit.PropertyRemap in project FastAsyncWorldEdit by IntellectualSites.
the class EditSessionBuilder method compile.
/**
* Compile the builder to the settings given. Prepares history, limits, lighting, etc.
*/
public EditSessionBuilder compile() {
if (compiled) {
return this;
}
compiled = true;
wrapped = false;
if (event == null) {
event = new EditSessionEvent(world, actor, -1, null);
}
if (actor == null && event.getActor() != null) {
actor = event.getActor();
}
if (limit == null) {
if (actor == null) {
limit = FaweLimit.MAX;
} else {
limit = actor.getLimit();
}
}
if (fastMode == null) {
if (actor == null) {
fastMode = !Settings.settings().HISTORY.ENABLE_FOR_CONSOLE;
} else {
fastMode = actor.getSession().hasFastMode();
}
}
if (checkMemory == null) {
checkMemory = actor != null && !this.fastMode;
}
if (checkMemory) {
if (MemUtil.isMemoryLimitedSlow()) {
if (Permission.hasPermission(actor, "worldedit.fast")) {
actor.print(Caption.of("fawe.info.worldedit.oom.admin"));
}
throw FaweCache.LOW_MEMORY;
}
}
// this.originalLimit = limit;
this.blockBag = limit.INVENTORY_MODE != 0 ? blockBag : null;
this.limit = limit.copy();
if (extent == null) {
IQueueExtent<IQueueChunk> queue = null;
World unwrapped = WorldWrapper.unwrap(world);
boolean placeChunks = (this.fastMode || this.limit.FAST_PLACEMENT) && (wnaMode == null || !wnaMode);
if (placeChunks) {
wnaMode = false;
if (unwrapped instanceof IQueueExtent) {
extent = queue = (IQueueExtent) unwrapped;
} else if (Settings.settings().QUEUE.PARALLEL_THREADS > 1 && !Fawe.isMainThread()) {
ParallelQueueExtent parallel = new ParallelQueueExtent(Fawe.instance().getQueueHandler(), world, fastMode);
queue = parallel.getExtent();
extent = parallel;
} else {
extent = queue = Fawe.instance().getQueueHandler().getQueue(world);
}
} else {
wnaMode = true;
extent = world;
}
if (combineStages == null) {
combineStages = // If it's enabled in the settings
Settings.settings().HISTORY.COMBINE_STAGES && // If fast placement is disabled, it's slower to perform a copy on each chunk
this.limit.FAST_PLACEMENT && // If the edit uses items from the inventory we can't use a delayed task
this.blockBag == null;
}
extent = this.bypassAll = wrapExtent(extent, eventBus, event, EditSession.Stage.BEFORE_CHANGE);
this.bypassHistory = this.extent = wrapExtent(bypassAll, eventBus, event, EditSession.Stage.BEFORE_REORDER);
if (!this.fastMode || changeSet != null) {
if (changeSet == null) {
if (Settings.settings().HISTORY.USE_DISK) {
UUID uuid = actor == null ? Identifiable.CONSOLE : actor.getUniqueId();
if (Settings.settings().HISTORY.USE_DATABASE) {
changeSet = new RollbackOptimizedHistory(world, uuid);
} else {
changeSet = new DiskStorageHistory(world, uuid);
}
// } else if (combineStages && Settings.settings().HISTORY.COMPRESSION_LEVEL == 0) {
// changeSet = new CPUOptimizedChangeSet(world);
} else {
if (combineStages && Settings.settings().HISTORY.COMPRESSION_LEVEL == 0) {
// TODO add CPUOptimizedChangeSet
}
changeSet = new MemoryOptimizedHistory(world);
}
}
if (this.limit.SPEED_REDUCTION > 0) {
this.extent = this.bypassHistory = new SlowExtent(this.bypassHistory, this.limit.SPEED_REDUCTION);
}
if (command != null && changeSet instanceof RollbackOptimizedHistory) {
((RollbackOptimizedHistory) changeSet).setCommand(this.command);
}
if (!(changeSet instanceof NullChangeSet)) {
if (this.blockBag != null) {
// TODO implement block bag as IBatchProcessor
changeSet = new BlockBagChangeSet(changeSet, blockBag, limit.INVENTORY_MODE == 1);
}
if (combineStages) {
changeTask = changeSet;
this.extent = extent.enableHistory(changeSet);
} else {
this.extent = new HistoryExtent(extent, changeSet);
// if (this.blockBag != null) {
// this.extent = new BlockBagExtent(this.extent, blockBag, limit.INVENTORY_MODE == 1);
// }
}
}
}
if (allowedRegions == null && Settings.settings().REGION_RESTRICTIONS) {
if (actor != null && !actor.hasPermission("fawe.bypass") && !actor.hasPermission("fawe.bypass.regions")) {
if (actor instanceof Player) {
Player player = (Player) actor;
allowedRegions = player.getAllowedRegions();
}
}
}
if (disallowedRegions == null && Settings.settings().REGION_RESTRICTIONS && Settings.settings().REGION_RESTRICTIONS_OPTIONS.ALLOW_BLACKLISTS) {
if (actor != null && !actor.hasPermission("fawe.bypass") && !actor.hasPermission("fawe.bypass.regions")) {
if (actor instanceof Player) {
Player player = (Player) actor;
disallowedRegions = player.getDisallowedRegions();
}
}
}
FaweRegionExtent regionExtent = null;
if (disallowedRegions != null) {
// Always use MultiRegionExtent if we have blacklist regions
regionExtent = new MultiRegionExtent(this.extent, this.limit, allowedRegions, disallowedRegions);
} else if (allowedRegions == null) {
allowedRegions = new Region[] { RegionWrapper.GLOBAL() };
} else {
if (allowedRegions.length == 0) {
regionExtent = new NullExtent(this.extent, FaweCache.NO_REGION);
} else {
if (allowedRegions.length == 1) {
regionExtent = new SingleRegionExtent(this.extent, this.limit, allowedRegions[0]);
} else {
regionExtent = new MultiRegionExtent(this.extent, this.limit, allowedRegions, null);
}
}
}
// There's no need to do lighting (and it'll also just be a pain to implement) if we're not placing chunks
if (placeChunks) {
if (((relightMode != null && relightMode != RelightMode.NONE) || (relightMode == null && Settings.settings().LIGHTING.MODE > 0))) {
relighter = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getRelighterFactory().createRelighter(relightMode, world, queue);
extent.addProcessor(new RelightProcessor(relighter));
}
extent.addProcessor(new HeightmapProcessor(world.getMinY(), world.getMaxY()));
} else {
relighter = NullRelighter.INSTANCE;
}
if (limit != null && !limit.isUnlimited() && regionExtent != null) {
this.extent = new LimitExtent(regionExtent, limit);
} else if (limit != null && !limit.isUnlimited()) {
this.extent = new LimitExtent(this.extent, limit);
} else if (regionExtent != null) {
this.extent = regionExtent;
}
if (this.limit != null && this.limit.STRIP_NBT != null && !this.limit.STRIP_NBT.isEmpty()) {
if (placeChunks) {
extent.addProcessor(new StripNBTExtent(this.extent, this.limit.STRIP_NBT));
} else {
this.extent = new StripNBTExtent(this.extent, this.limit.STRIP_NBT);
}
}
if (this.limit != null && !this.limit.isUnlimited()) {
Set<String> limitBlocks = new HashSet<>();
if ((getActor() == null || getActor().hasPermission("worldedit.anyblock") && this.limit.UNIVERSAL_DISALLOWED_BLOCKS)) {
limitBlocks.addAll(WorldEdit.getInstance().getConfiguration().disallowedBlocks);
}
if (this.limit.DISALLOWED_BLOCKS != null && !this.limit.DISALLOWED_BLOCKS.isEmpty()) {
limitBlocks.addAll(this.limit.DISALLOWED_BLOCKS);
}
Set<PropertyRemap<?>> remaps = this.limit.REMAP_PROPERTIES;
if (!limitBlocks.isEmpty() || (remaps != null && !remaps.isEmpty())) {
if (placeChunks) {
extent.addProcessor(new DisallowedBlocksExtent(this.extent, limitBlocks, remaps));
} else {
this.extent = new DisallowedBlocksExtent(this.extent, limitBlocks, remaps);
}
}
}
this.extent = wrapExtent(this.extent, eventBus, event, EditSession.Stage.BEFORE_HISTORY);
}
return this;
}
Aggregations