From 334d5cfaabefcf8d7dd802eea8bcc29e1d18b4e3 Mon Sep 17 00:00:00 2001 From: wizjany Date: Sat, 18 May 2019 09:52:24 -0400 Subject: [PATCH] The "spawner|mobType" syntax once again makes functional mob spawners. Also fix block parser context not being restricted. --- .../bukkit/BukkitServerInterface.java | 5 +- .../worldedit/blocks/MobSpawnerBlock.java | 58 +++++++++++-------- .../command/argument/FactoryConverter.java | 1 + .../factory/parser/DefaultBlockParser.java | 11 ++-- .../extension/platform/Platform.java | 2 +- 5 files changed, 45 insertions(+), 32 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java index e4707341a..ccc025d8c 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java @@ -86,7 +86,10 @@ public class BukkitServerInterface implements MultiUserPlatform { @Override public boolean isValidMobType(String type) { - final EntityType entityType = EntityType.fromName(type); + if (!type.startsWith("minecraft:")) { + return false; + } + final EntityType entityType = EntityType.fromName(type.substring(10)); return entityType != null && entityType.isAlive(); } diff --git a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java index eb0c2e382..cde191e64 100644 --- a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java +++ b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java @@ -19,7 +19,10 @@ package com.sk89q.worldedit.blocks; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.sk89q.jnbt.CompoundTag; +import com.sk89q.jnbt.IntTag; import com.sk89q.jnbt.ListTag; import com.sk89q.jnbt.NBTUtils; import com.sk89q.jnbt.ShortTag; @@ -38,17 +41,17 @@ import java.util.Map; public class MobSpawnerBlock extends BaseBlock { private String mobType; - private short delay; + private short delay = -1; // advanced mob spawner features - private short spawnCount; - private short spawnRange; + private short spawnCount = 4; + private short spawnRange = 4; private CompoundTag spawnData; private ListTag spawnPotentials; - private short minSpawnDelay; - private short maxSpawnDelay; - private short maxNearbyEntities; - private short requiredPlayerRange; + private short minSpawnDelay = 200; + private short maxSpawnDelay = 800; + private short maxNearbyEntities = 6; + private short requiredPlayerRange = 16; /** * Construct the mob spawner block with a specified data value. @@ -119,7 +122,6 @@ public class MobSpawnerBlock extends BaseBlock { @Override public CompoundTag getNbtData() { Map values = new HashMap<>(); - values.put("EntityId", new StringTag(mobType)); values.put("Delay", new ShortTag(delay)); values.put("SpawnCount", new ShortTag(spawnCount)); values.put("SpawnRange", new ShortTag(spawnRange)); @@ -127,10 +129,16 @@ public class MobSpawnerBlock extends BaseBlock { values.put("MaxSpawnDelay", new ShortTag(maxSpawnDelay)); values.put("MaxNearbyEntities", new ShortTag(maxNearbyEntities)); values.put("RequiredPlayerRange", new ShortTag(requiredPlayerRange)); - if (spawnData != null) { + if (spawnData == null) { + values.put("SpawnData", new CompoundTag(ImmutableMap.of("id", new StringTag(mobType)))); + } else { values.put("SpawnData", new CompoundTag(spawnData.getValue())); } - if (spawnPotentials != null) { + if (spawnPotentials == null) { + values.put("SpawnPotentials", new ListTag(CompoundTag.class, ImmutableList.of( + new CompoundTag(ImmutableMap.of("Weight", new IntTag(1), "Entity", + new CompoundTag(ImmutableMap.of("id", new StringTag(mobType)))))))); + } else { values.put("SpawnPotentials", new ListTag(CompoundTag.class, spawnPotentials.getValue())); } @@ -150,18 +158,26 @@ public class MobSpawnerBlock extends BaseBlock { throw new RuntimeException("'MobSpawner' tile entity expected"); } - StringTag mobTypeTag; + CompoundTag spawnDataTag; + String mobType; ShortTag delayTag; try { - mobTypeTag = NBTUtils.getChildTag(values, "EntityId", StringTag.class); - delayTag = NBTUtils.getChildTag(values, "Delay", ShortTag.class); + spawnDataTag = NBTUtils.getChildTag(values, "SpawnData", CompoundTag.class); + mobType = spawnDataTag.getString("id"); + if (mobType.equals("")) { + throw new InvalidFormatException("No spawn id."); + } + this.mobType = mobType; } catch (InvalidFormatException ignored) { - throw new RuntimeException("Invalid mob spawner data: no EntityId and/or no Delay"); + throw new RuntimeException("Invalid mob spawner data: no SpawnData and/or no Delay"); + } + try { + delayTag = NBTUtils.getChildTag(values, "Delay", ShortTag.class); + this.delay = delayTag.getValue(); + } catch (InvalidFormatException ignored) { + this.delay = -1; } - - this.mobType = mobTypeTag.getValue(); - this.delay = delayTag.getValue(); ShortTag spawnCountTag = null; ShortTag spawnRangeTag = null; @@ -170,7 +186,6 @@ public class MobSpawnerBlock extends BaseBlock { ShortTag maxNearbyEntitiesTag = null; ShortTag requiredPlayerRangeTag = null; ListTag spawnPotentialsTag = null; - CompoundTag spawnDataTag = null; try { spawnCountTag = NBTUtils.getChildTag(values, "SpawnCount", ShortTag.class); } catch (InvalidFormatException ignored) { @@ -199,10 +214,6 @@ public class MobSpawnerBlock extends BaseBlock { spawnPotentialsTag = NBTUtils.getChildTag(values, "SpawnPotentials", ListTag.class); } catch (InvalidFormatException ignored) { } - try { - spawnDataTag = NBTUtils.getChildTag(values, "SpawnData", CompoundTag.class); - } catch (InvalidFormatException ignored) { - } if (spawnCountTag != null) { this.spawnCount = spawnCountTag.getValue(); @@ -225,9 +236,6 @@ public class MobSpawnerBlock extends BaseBlock { if (spawnPotentialsTag != null) { this.spawnPotentials = new ListTag(CompoundTag.class, spawnPotentialsTag.getValue()); } - if (spawnDataTag != null) { - this.spawnData = new CompoundTag(spawnDataTag.getValue()); - } } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java index 0de24e5a7..b3b2b8108 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java @@ -81,6 +81,7 @@ public class FactoryConverter implements ArgumentConverter { } } parserContext.setSession(session); + parserContext.setRestricted(true); try { return SuccessfulConversion.fromSingle( diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java index a103318ef..f0a14bee7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java @@ -46,6 +46,8 @@ import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.FuzzyBlockState; +import com.sk89q.worldedit.world.entity.EntityType; +import com.sk89q.worldedit.world.entity.EntityTypes; import com.sk89q.worldedit.world.registry.LegacyMapper; import java.util.HashMap; @@ -323,12 +325,11 @@ public class DefaultBlockParser extends InputParser { // Allow setting mob spawn type if (blockAndExtraData.length > 1) { String mobName = blockAndExtraData[1]; - for (MobType mobType : MobType.values()) { - if (mobType.getName().toLowerCase(Locale.ROOT).equals(mobName.toLowerCase(Locale.ROOT))) { - mobName = mobType.getName(); - break; - } + EntityType ent = EntityTypes.get(mobName.toLowerCase(Locale.ROOT)); + if (ent == null) { + throw new NoMatchException("Unknown entity type '" + mobName + "'"); } + mobName = ent.getId(); if (!worldEdit.getPlatformManager().queryCapability(Capability.USER_COMMANDS).isValidMobType(mobName)) { throw new NoMatchException("Unknown mob type '" + mobName + "'"); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Platform.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Platform.java index d9391ddc3..fcbd6ff29 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Platform.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Platform.java @@ -63,7 +63,7 @@ public interface Platform { * Checks if a mob type is valid. * * @param type The mob type name to check - * @return Whether the name is a valid mod bype + * @return Whether the name is a valid mod type */ boolean isValidMobType(String type);