Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
206 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/PlacementConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* 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 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.sk89q.worldedit.command.argument; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Iterables; | ||
import com.sk89q.worldedit.IncompleteRegionException; | ||
import com.sk89q.worldedit.extension.platform.Actor; | ||
import com.sk89q.worldedit.math.BlockVector3; | ||
import com.sk89q.worldedit.regions.RegionSelector; | ||
import com.sk89q.worldedit.session.Placement; | ||
import com.sk89q.worldedit.util.formatting.text.Component; | ||
import com.sk89q.worldedit.util.formatting.text.TextComponent; | ||
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; | ||
import org.enginehub.piston.CommandManager; | ||
import org.enginehub.piston.converter.ArgumentConverter; | ||
import org.enginehub.piston.converter.ConversionResult; | ||
import org.enginehub.piston.converter.SuccessfulConversion; | ||
import org.enginehub.piston.inject.InjectedValueAccess; | ||
import org.enginehub.piston.inject.Key; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class PlacementConverter implements ArgumentConverter<Placement> { | ||
|
||
private static final List<String> FIXED_SUGGESTIONS = Arrays.asList( | ||
"pos1", | ||
"player" | ||
); | ||
|
||
public static void register(CommandManager commandManager) { | ||
commandManager.registerConverter( | ||
Key.of(Placement.class), | ||
new PlacementConverter() | ||
); | ||
} | ||
private final VectorConverter<Integer, BlockVector3> vectorConverter = VectorConverter.BLOCK_VECTOR_3_CONVERTER; | ||
|
||
@Override | ||
public Component describeAcceptableArguments() { | ||
return TextComponent.builder() | ||
.append(String.join(", ", FIXED_SUGGESTIONS)) | ||
.append(", or ") | ||
.append(vectorConverter.describeAcceptableArguments()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public List<String> getSuggestions(String input, InjectedValueAccess context) { | ||
return ImmutableList.copyOf(Iterables.concat( | ||
FIXED_SUGGESTIONS, | ||
vectorConverter.getSuggestions(input, context) | ||
)); | ||
} | ||
|
||
@Override | ||
public ConversionResult<Placement> convert(String input, InjectedValueAccess context) { | ||
switch (input) { | ||
case "pos1": | ||
return SuccessfulConversion.fromSingle(Placement.POS1); | ||
|
||
case "player": | ||
return SuccessfulConversion.fromSingle(Placement.PLAYER); | ||
|
||
default: | ||
if (input.startsWith("^")) { | ||
return vectorConverter.convert(input.substring(1), context) | ||
.map(collection -> collection.stream() | ||
.map(vector -> new Placement() { | ||
@Override | ||
public BlockVector3 getPlacementPosition(RegionSelector selector, Actor actor) throws IncompleteRegionException { | ||
return Placement.PLAYER.getPlacementPosition(selector, actor).add(vector); | ||
} | ||
|
||
@Override | ||
public Component getInfo() { | ||
return TranslatableComponent.of("worldedit.toggleplace.relative-player", | ||
TextComponent.of(vector.getX()), | ||
TextComponent.of(vector.getY()), | ||
TextComponent.of(vector.getZ()) | ||
); | ||
} | ||
}) | ||
.collect(Collectors.toList()) | ||
); | ||
} else { | ||
return vectorConverter.convert(input, context) | ||
.map(collection -> collection.stream() | ||
.map(vector -> new Placement() { | ||
@Override | ||
public BlockVector3 getPlacementPosition(RegionSelector selector, Actor actor) throws IncompleteRegionException { | ||
return vector; | ||
} | ||
|
||
@Override | ||
public Component getInfo() { | ||
return TranslatableComponent.of("worldedit.toggleplace.absolute", | ||
TextComponent.of(vector.getX()), | ||
TextComponent.of(vector.getY()), | ||
TextComponent.of(vector.getZ()) | ||
); | ||
} | ||
}) | ||
.collect(Collectors.toList()) | ||
); | ||
} | ||
} | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
worldedit-core/src/main/java/com/sk89q/worldedit/command/util/Placement.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
worldedit-core/src/main/java/com/sk89q/worldedit/session/Placement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.sk89q.worldedit.session; | ||
|
||
import com.sk89q.worldedit.IncompleteRegionException; | ||
import com.sk89q.worldedit.LocalSession; | ||
import com.sk89q.worldedit.extension.platform.Actor; | ||
import com.sk89q.worldedit.extension.platform.Locatable; | ||
import com.sk89q.worldedit.math.BlockVector3; | ||
import com.sk89q.worldedit.regions.RegionSelector; | ||
import com.sk89q.worldedit.util.formatting.text.Component; | ||
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
public interface Placement { | ||
Placement PLAYER = new Placement() { | ||
@Override | ||
public BlockVector3 getPlacementPosition(RegionSelector selector, Actor actor) throws IncompleteRegionException { | ||
checkNotNull(actor); | ||
if (actor instanceof Locatable) { | ||
return ((Locatable) actor).getBlockLocation().toVector().toBlockPoint(); | ||
} else { | ||
throw new IncompleteRegionException(); | ||
} | ||
} | ||
|
||
@Override | ||
public Component getInfo() { | ||
return TranslatableComponent.of("worldedit.toggleplace.player"); | ||
} | ||
}; | ||
|
||
Placement POS1 = new Placement() { | ||
@Override | ||
public BlockVector3 getPlacementPosition(RegionSelector selector, Actor actor) throws IncompleteRegionException { | ||
return selector.getPrimaryPosition(); | ||
} | ||
|
||
@Override | ||
public Component getInfo() { | ||
return TranslatableComponent.of("worldedit.toggleplace.pos1"); | ||
} | ||
}; | ||
|
||
BlockVector3 getPlacementPosition(RegionSelector selector, Actor actor) throws IncompleteRegionException; | ||
|
||
Component getInfo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters