Skip to content

Commit

Permalink
Add /setblock command (related to #499)
Browse files Browse the repository at this point in the history
  • Loading branch information
aramperes committed Nov 30, 2017
1 parent aab7327 commit 6d00594
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/glowstone/GlowServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ private void loadPlugins() {
commandMap.register("minecraft", new EnchantCommand());
commandMap.register("minecraft", new TestForCommand());
commandMap.register("minecraft", new TestForBlockCommand());
commandMap.register("minecraft", new SetBlockCommand());

File folder = new File(config.getString(Key.PLUGIN_FOLDER));
if (!folder.isDirectory() && !folder.mkdirs()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/glowstone/block/state/BlockStateData.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
public class BlockStateData {

private final Map<String, String> map = Maps.newHashMap();
private final int numericValue;
private final byte numericValue;

public BlockStateData(int numericValue) {
public BlockStateData(byte numericValue) {
this.numericValue = numericValue;
}

Expand Down Expand Up @@ -37,7 +37,7 @@ public boolean isNumeric() {
return numericValue != -1;
}

public int getNumericValue() {
public byte getNumericValue() {
return numericValue;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/glowstone/command/CommandUtils.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package net.glowstone.command;

import net.glowstone.GlowWorld;
import net.glowstone.block.GlowBlock;
import net.glowstone.block.state.BlockStateData;
import net.glowstone.block.state.InvalidBlockStateException;
import net.glowstone.block.state.StateSerialization;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
Expand Down Expand Up @@ -98,12 +98,12 @@ private static double getDouble(String d, boolean shift) {
return Double.valueOf(d);
}

public static BlockStateData readState(CommandSender sender, GlowBlock block, String state) {
public static BlockStateData readState(CommandSender sender, Material type, String state) {
if (isNumeric(state)) {
return new BlockStateData(Integer.parseInt(state));
return new BlockStateData(Byte.parseByte(state));
}
try {
return StateSerialization.parse(block.getType(), state);
return StateSerialization.parse(type, state);
} catch (InvalidBlockStateException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
return null;
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/net/glowstone/command/minecraft/SetBlockCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package net.glowstone.command.minecraft;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.state.BlockStateData;
import net.glowstone.block.state.InvalidBlockStateException;
import net.glowstone.block.state.StateSerialization;
import net.glowstone.command.CommandUtils;
import net.glowstone.constants.ItemIds;
import net.glowstone.util.mojangson.Mojangson;
import net.glowstone.util.mojangson.ex.MojangsonParseException;
import net.glowstone.util.nbt.CompoundTag;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.VanillaCommand;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SetBlockCommand extends VanillaCommand {
public SetBlockCommand() {
super("setblock",
"Changes a block to another block.",
"/setblock <x> <y> <z> <block> [dataValue|state] [dataTag]",
Collections.emptyList());
setPermission("minecraft.command.setblock");
}

@Override
public boolean execute(CommandSender sender, String label, String[] args) {
if (!testPermission(sender)) {
return false;
}
if (args.length < 4) {
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
String itemName = args[3].toLowerCase();
if (!itemName.startsWith("minecraft:")) {
itemName = "minecraft:" + itemName;
}
Material type = ItemIds.getBlock(itemName);
if (type == null) {
sender.sendMessage(ChatColor.RED + itemName + " is not a valid block type");
return false;
}
Location location = CommandUtils.getLocation(CommandUtils.getLocation(sender), args[0], args[1], args[2]);
GlowBlock block = (GlowBlock) location.getBlock();
byte dataValue = 0;
if (args.length > 4) {
String state = args[4];
BlockStateData data = CommandUtils.readState(sender, type, state);
if (data == null) {
return false;
}
if (data.isNumeric()) {
dataValue = data.getNumericValue();
} else {
try {
dataValue = StateSerialization.parseData(type, data).getData();
} catch (InvalidBlockStateException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
return false;
}
}
}
block.setType(type, dataValue, true);
if (args.length > 5 && block.getBlockEntity() != null) {
String dataTag = String.join(" ", new ArrayList<>(Arrays.asList(args)).subList(5, args.length));
try {
CompoundTag prev = new CompoundTag();
block.getBlockEntity().saveNbt(prev);
CompoundTag tag = Mojangson.parseCompound(dataTag);
tag.mergeInto(prev, true);
block.getBlockEntity().loadNbt(prev);
} catch (MojangsonParseException e) {
sender.sendMessage(ChatColor.RED + "Invalid Data Tag: " + e.getMessage());
return false;
}
}
sender.sendMessage("Block placed");
return true;
}

@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
if (args.length == 4) {
return ItemIds.getTabCompletion(args[3]);
}
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ public boolean execute(CommandSender sender, String label, String[] args) {
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
String itemName = args[3];
String itemName = args[3].toLowerCase();
if (!itemName.startsWith("minecraft:")) {
itemName = "minecraft:" + itemName;
}
Material type = ItemIds.getItem(itemName);
Material type = ItemIds.getBlock(itemName);
if (type == null) {
sender.sendMessage(ChatColor.RED + itemName + " is not a valid block type.");
}
Location location = CommandUtils.getLocation(CommandUtils.getLocation(sender), args[0], args[1], args[2]);
GlowBlock block = (GlowBlock) location.getBlock();
if (block.getType() != type) {
Expand All @@ -52,7 +55,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
}
if (args.length > 4) {
String state = args[4];
BlockStateData data = CommandUtils.readState(sender, block, state);
BlockStateData data = CommandUtils.readState(sender, block.getType(), state);
if (data == null) {
return false;
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/net/glowstone/util/nbt/CompoundTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void remove(String key) {

/**
* Checks to see if this tag is a strict, deep submap of the given CompoundTag.
*
* @param other The CompoundTag that should contain our values.
* @return
*/
Expand Down Expand Up @@ -99,6 +100,21 @@ public boolean matches(CompoundTag other) {
return true;
}

/**
* Merges the contents of this compound into the supplied compound.
*
* @param other the other compound to merge into.
* @param overwrite whether keys already set in the other compound should be overwritten.
*/
public void mergeInto(CompoundTag other, boolean overwrite) {
for (String key : value.keySet()) {
if (!overwrite && other.containsKey(key)) {
continue;
}
other.put(key, value.get(key));
}
}

////////////////////////////////////////////////////////////////////////////
// Simple gets

Expand Down

0 comments on commit 6d00594

Please sign in to comment.