Skip to content

Commit

Permalink
Added a //world command to set the override
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Jul 27, 2019
1 parent ab25732 commit 2dc1d11
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 1 deletion.
Expand Up @@ -159,6 +159,11 @@ public String getName() {
return getWorld().getName();
}

@Override
public String getId() {
return getWorld().getName().replace(" ", "_").toLowerCase();
}

@Override
public Path getStoragePath() {
return getWorld().getWorldFolder().toPath();
Expand Down
Expand Up @@ -39,6 +39,7 @@
import com.sk89q.worldedit.world.block.BlockStateHolder;

import java.util.List;
import java.util.Locale;

import javax.annotation.Nullable;

Expand All @@ -57,6 +58,11 @@ public String getName() {
return this.name;
}

@Override
public String getId() {
return getName().replace(" ", "_").toLowerCase(Locale.ROOT);
}

@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block, boolean notifyAndLight)
throws WorldEditException {
Expand Down
Expand Up @@ -33,6 +33,7 @@
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.util.formatting.component.PaginationBox;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.item.ItemType;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
Expand Down Expand Up @@ -182,6 +183,21 @@ public void drawSelection(Player player, LocalSession session,
}
}

@Command(
name = "/world",
desc = "Sets the world override"
)
@CommandPermissions("worldedit.world")
public void world(Actor actor, LocalSession session,
@Arg(desc = "The world override", def = "") World world) {
session.setWorldOverride(world);
if (world == null) {
actor.print("Removed world override.");
} else {
actor.print("Set the world override to " + world.getId() + ". (Use //world to go back to default)");
}
}

@Command(
name = "gmask",
aliases = {"/gmask"},
Expand Down
@@ -0,0 +1,77 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.command.argument;

import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.world.World;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.converter.ArgumentConverter;
import org.enginehub.piston.converter.ConversionResult;
import org.enginehub.piston.converter.FailedConversion;
import org.enginehub.piston.converter.SuccessfulConversion;
import org.enginehub.piston.inject.InjectedValueAccess;
import org.enginehub.piston.inject.Key;

import java.util.List;
import java.util.stream.Collectors;

public class WorldConverter implements ArgumentConverter<World> {

public static void register(CommandManager commandManager) {
commandManager.registerConverter(Key.of(World.class),
new WorldConverter()
);
}

private final TextComponent choices;

private WorldConverter() {
this.choices = TextComponent.of("any world");
}

@Override
public Component describeAcceptableArguments() {
return this.choices;
}

@Override
public List<String> getSuggestions(String input) {
return WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getWorlds().stream()
.map(World::getId)
.filter(world -> world.startsWith(input))
.collect(Collectors.toList());
}

@Override
public ConversionResult<World> convert(String s, InjectedValueAccess injectedValueAccess) {
World result = WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getWorlds().stream()
.filter(world -> world.getId().equals(s))
.findAny().orElse(null);
return result == null
? FailedConversion.from(new IllegalArgumentException(
"Not a valid world: " + s))
: SuccessfulConversion.fromSingle(result);
}
}
Expand Up @@ -82,6 +82,7 @@
import com.sk89q.worldedit.command.argument.RegionFactoryConverter;
import com.sk89q.worldedit.command.argument.RegistryConverter;
import com.sk89q.worldedit.command.argument.VectorConverter;
import com.sk89q.worldedit.command.argument.WorldConverter;
import com.sk89q.worldedit.command.argument.ZonedDateTimeConverter;
import com.sk89q.worldedit.command.util.PermissionCondition;
import com.sk89q.worldedit.command.util.SubCommandPermissionCondition;
Expand Down Expand Up @@ -219,6 +220,7 @@ private void registerArgumentConverters() {
BooleanConverter.register(commandManager);
EntityRemoverConverter.register(commandManager);
RegionFactoryConverter.register(commandManager);
WorldConverter.register(commandManager);
}

private void registerAlwaysInjectedValues() {
Expand Down
Expand Up @@ -61,6 +61,11 @@ public String getName() {
return "null";
}

@Override
public String getId() {
return "null";
}

@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block, boolean notifyAndLight) throws WorldEditException {
return false;
Expand Down
Expand Up @@ -31,6 +31,7 @@
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.TreeGenerator;
import com.sk89q.worldedit.world.block.BlockState;
Expand All @@ -44,7 +45,7 @@
/**
* Represents a world (dimension).
*/
public interface World extends Extent {
public interface World extends Extent, Keyed {

/**
* Get the name of the world.
Expand Down
Expand Up @@ -100,6 +100,7 @@
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Random;
Expand Down Expand Up @@ -168,6 +169,13 @@ public String getName() {
return getWorld().getLevelProperties().getLevelName();
}

@Override
public String getId() {
return getWorld().getLevelProperties().getLevelName()
.replace(" ", "_").toLowerCase(Locale.ROOT)
+ getWorld().dimension.getType().getSuffix();
}

@Override
public Path getStoragePath() {
final World world = getWorld();
Expand Down
Expand Up @@ -93,6 +93,7 @@
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.storage.SaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.common.DimensionManager;

import javax.annotation.Nullable;
import java.io.File;
Expand Down Expand Up @@ -169,6 +170,11 @@ public String getName() {
return getWorld().getWorldInfo().getWorldName();
}

@Override
public String getId() {
return DimensionManager.getRegistry().getKey(getWorld().dimension.getType()).toString();
}

@Override
public Path getStoragePath() {
final World world = getWorld();
Expand Down
Expand Up @@ -60,6 +60,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -117,6 +118,12 @@ public String getName() {
return getWorld().getName();
}

@Override
public String getId() {
return getName().replace(" ", "_").toLowerCase(Locale.ROOT) +
getWorld().getDimension().getType().getName().toLowerCase(Locale.ROOT);
}

@Override
public Path getStoragePath() {
return getWorld().getDirectory();
Expand Down

0 comments on commit 2dc1d11

Please sign in to comment.