Skip to content

Commit e162c2f

Browse files
committed
Support setting spawner type to null
1 parent 436c4a0 commit e162c2f

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCCreatureSpawner.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.laytonsmith.abstraction.MCCreatureSpawner;
44
import com.laytonsmith.abstraction.bukkit.blocks.BukkitMCBlockState;
55
import com.laytonsmith.abstraction.enums.MCEntityType;
6+
import com.laytonsmith.abstraction.enums.MCVersion;
67
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCEntityType;
8+
import com.laytonsmith.core.Static;
79
import org.bukkit.block.CreatureSpawner;
810
import org.bukkit.entity.EntityType;
911

@@ -28,9 +30,14 @@ public MCEntityType getSpawnedType() {
2830
@Override
2931
public void setSpawnedType(MCEntityType type) {
3032
if(type == null) {
31-
return; // unsupported at this time
33+
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_20)) {
34+
cs.setSpawnedType(null);
35+
} else {
36+
cs.setSpawnedType(EntityType.PIG); // null unsupported
37+
}
38+
} else {
39+
cs.setSpawnedType(((BukkitMCEntityType) type).getConcrete());
3240
}
33-
cs.setSpawnedType(((BukkitMCEntityType) type).getConcrete());
3441
cs.update();
3542
}
3643

src/main/java/com/laytonsmith/core/functions/Minecraft.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.laytonsmith.abstraction.StaticLayer;
1616
import com.laytonsmith.abstraction.blocks.MCBlockData;
1717
import com.laytonsmith.abstraction.blocks.MCBlockFace;
18+
import com.laytonsmith.abstraction.blocks.MCBlockState;
1819
import com.laytonsmith.abstraction.blocks.MCMaterial;
1920
import com.laytonsmith.abstraction.enums.MCEffect;
2021
import com.laytonsmith.abstraction.enums.MCEntityType;
@@ -888,15 +889,15 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
888889
w = p.getWorld();
889890
}
890891
MCLocation location = ObjectGenerator.GetGenerator().location(args[0], w, t);
891-
if(location.getBlock().getState() instanceof MCCreatureSpawner) {
892-
MCEntityType entityType = ((MCCreatureSpawner) location.getBlock().getState()).getSpawnedType();
893-
if(entityType == null) {
894-
return CNull.NULL;
895-
}
896-
return new CString(entityType.name(), t);
897-
} else {
898-
throw new CREFormatException("The block at " + location.toString() + " is not a spawner block", t);
892+
MCBlockState state = location.getBlock().getState();
893+
if(!(state instanceof MCCreatureSpawner)) {
894+
throw new CREFormatException("The block at " + location + " is not a spawner block", t);
895+
}
896+
MCEntityType entityType = ((MCCreatureSpawner) state).getSpawnedType();
897+
if(entityType == null) {
898+
return CNull.NULL;
899899
}
900+
return new CString(entityType.name(), t);
900901
}
901902

902903
@Override
@@ -911,7 +912,8 @@ public Integer[] numArgs() {
911912

912913
@Override
913914
public String docs() {
914-
return "string {locationArray} Gets the entity type that will spawn from the specified mob spawner.";
915+
return "string {locationArray} Gets the entity type that will spawn from the specified mob spawner."
916+
+ " May be null.";
915917
}
916918

917919
@Override
@@ -947,18 +949,21 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
947949
w = p.getWorld();
948950
}
949951
MCLocation location = ObjectGenerator.GetGenerator().location(args[0], w, t);
950-
MCEntityType type;
951-
try {
952-
type = MCEntityType.valueOf(args[1].val().toUpperCase());
953-
} catch (IllegalArgumentException iae) {
954-
throw new CREBadEntityException("Not a registered entity type: " + args[1].val(), t);
952+
MCBlockState state = location.getBlock().getState();
953+
if(!(state instanceof MCCreatureSpawner)) {
954+
throw new CREFormatException("The block at " + location + " is not a spawner block", t);
955955
}
956-
if(location.getBlock().getState() instanceof MCCreatureSpawner) {
957-
((MCCreatureSpawner) location.getBlock().getState()).setSpawnedType(type);
956+
if(args[1] instanceof CNull) {
957+
((MCCreatureSpawner) state).setSpawnedType(null);
958958
return CVoid.VOID;
959-
} else {
960-
throw new CREFormatException("The block at " + location.toString() + " is not a spawner block", t);
961959
}
960+
try {
961+
MCEntityType type = MCEntityType.valueOf(args[1].val().toUpperCase());
962+
((MCCreatureSpawner) state).setSpawnedType(type);
963+
} catch (IllegalArgumentException iae) {
964+
throw new CREBadEntityException("Not a valid entity type: " + args[1].val(), t);
965+
}
966+
return CVoid.VOID;
962967
}
963968

964969
@Override
@@ -974,7 +979,8 @@ public Integer[] numArgs() {
974979
@Override
975980
public String docs() {
976981
return "void {locationArray, type} Sets the mob spawner's entity type at the location specified."
977-
+ " If the location is not a mob spawner, or if the type is invalid, a FormatException is thrown."
982+
+ " If the location is not a mob spawner, a FormatException is thrown."
983+
+ " Entity type may be set to null. If the entity type is invalid, a BadEntityException is thrown."
978984
+ " ---- The type may be one of either "
979985
+ StringUtils.Join(MCEntityType.MCVanillaEntityType.values(), ", ", ", or ");
980986
}

0 commit comments

Comments
 (0)