add /set
This commit is contained in:
parent
b3f7b5a07b
commit
b6242d5de1
5 changed files with 98 additions and 1 deletions
|
@ -279,5 +279,7 @@ public class CommandEnvironment {
|
|||
this.registerExecutable(new CommandEffect());
|
||||
this.registerExecutable(new CommandExterminatus());
|
||||
this.registerExecutable(new CommandReset());
|
||||
|
||||
this.registerExecutable(new CommandSet());
|
||||
}
|
||||
}
|
||||
|
|
87
server/src/main/java/server/command/commands/CommandSet.java
Normal file
87
server/src/main/java/server/command/commands/CommandSet.java
Normal file
|
@ -0,0 +1,87 @@
|
|||
package server.command.commands;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import common.block.Block;
|
||||
import common.collect.Iterables;
|
||||
import common.collect.Lists;
|
||||
import common.init.BlockRegistry;
|
||||
import common.tags.TagObject;
|
||||
import common.tileentity.TileEntity;
|
||||
import common.util.BlockPos.MutableBlockPos;
|
||||
import common.world.State;
|
||||
import server.command.Command;
|
||||
import server.command.CommandEnvironment;
|
||||
import server.command.Executor;
|
||||
import server.command.RunException;
|
||||
import server.command.StringCompleter;
|
||||
import server.network.Player;
|
||||
import server.world.WorldServer;
|
||||
|
||||
public class CommandSet extends Command {
|
||||
public CommandSet() {
|
||||
super("set");
|
||||
|
||||
this.setParamsOptional();
|
||||
this.addString("block", false, new StringCompleter() {
|
||||
public Collection<String> complete(CommandEnvironment env, String last) {
|
||||
int idx = last.indexOf(',');
|
||||
if(idx >= 0) {
|
||||
Block block = BlockRegistry.byNameExact(last.substring(0, idx));
|
||||
if(block != null)
|
||||
return Lists.newArrayList(Iterables.transform(block.getSavedStates(), state -> BlockRegistry.getName(state)));
|
||||
}
|
||||
return BlockRegistry.getKeys();
|
||||
}
|
||||
});
|
||||
this.addTag("tag", 't');
|
||||
}
|
||||
|
||||
public Object exec(CommandEnvironment env, Executor exec, String block, TagObject tag) {
|
||||
if(!exec.isPlayer() || ((Player)exec).getPresentEntity() == null)
|
||||
throw new RunException("Nur Spieler können diesen Befehl ausführen");
|
||||
Player player = (Player)exec;
|
||||
Iterable<MutableBlockPos> blocks = player.getSelectedBlocks();
|
||||
if(blocks == null)
|
||||
throw new RunException("Es ist keine Auswahl vorhanden");
|
||||
State state;
|
||||
if(block == null) {
|
||||
if(player.getPresentEntity().getHeldItem() == null)
|
||||
throw new RunException("Es befindet sich kein Gegenstand in der Hand");
|
||||
else if(player.getPresentEntity().getHeldItem().getItem().getBlock() == null)
|
||||
throw new RunException("Der Gegenstand in der Hand ist kein Block");
|
||||
state = player.getPresentEntity().getHeldItem().getItem().getBlock().getState();
|
||||
}
|
||||
else {
|
||||
state = BlockRegistry.byName(block, null);
|
||||
if(state == null)
|
||||
throw new RunException("Block '%s' existiert nicht", block);
|
||||
}
|
||||
WorldServer world = (WorldServer)player.getPresentEntity().getServerWorld();
|
||||
int changed = 0;
|
||||
for(MutableBlockPos pos : blocks) {
|
||||
boolean success = world.setState(pos, state);
|
||||
if(tag != null) {
|
||||
TileEntity tile = world.getTileEntity(pos);
|
||||
if(tile != null) {
|
||||
TagObject te = new TagObject();
|
||||
tile.writeTags(te);
|
||||
te.merge(tag);
|
||||
TileEntity newTile = TileEntity.createAndLoadEntity(world, world.getChunk(pos), pos, te);
|
||||
if(newTile != null) {
|
||||
world.removeTileEntity(pos);
|
||||
world.setTileEntity(pos, newTile);
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(success)
|
||||
++changed;
|
||||
}
|
||||
if(changed > 0)
|
||||
exec.log("%d * %s platziert", changed, state.getBlock().getDisplay());
|
||||
else
|
||||
exec.log("Es wurden keine Blöcke verändert");
|
||||
return changed;
|
||||
}
|
||||
}
|
|
@ -106,6 +106,7 @@ import common.tileentity.TileEntity;
|
|||
import common.tileentity.TileEntityDevice;
|
||||
import common.tileentity.TileEntitySign;
|
||||
import common.util.BlockPos;
|
||||
import common.util.BlockPos.MutableBlockPos;
|
||||
import common.util.BoundingBox;
|
||||
import common.util.ChunkPos;
|
||||
import common.util.ExtMath;
|
||||
|
@ -1486,6 +1487,13 @@ public class Player extends User implements Executor, IPlayer
|
|||
return newValue;
|
||||
}
|
||||
|
||||
public Iterable<MutableBlockPos> getSelectedBlocks() {
|
||||
if(this.selectionDim == Integer.MIN_VALUE || this.selectionDim != UniverseRegistry.getId(this.entity.worldObj.dimension) ||
|
||||
this.selPos1 == null || this.selPos2 == null)
|
||||
return null;
|
||||
return BlockPos.getAllInBoxMutable(this.selPos1, this.selPos2);
|
||||
}
|
||||
|
||||
private boolean copyClipboard() {
|
||||
if(this.selectionDim == Integer.MIN_VALUE || this.selectionDim != UniverseRegistry.getId(this.entity.worldObj.dimension) ||
|
||||
this.selPos1 == null || this.selPos2 == null)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue