Skip to content

Commit

Permalink
BiomeTag rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 18, 2021
1 parent 52543bb commit 6163cc0
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 74 deletions.
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.events.player;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.BiomeTag;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.Utilities;
Expand Down Expand Up @@ -123,8 +124,8 @@ public void onPlayerEntersExitsBiome(PlayerMoveEvent event) {
}
from = new LocationTag(event.getFrom());
to = new LocationTag(event.getTo());
old_biome = new BiomeTag(from.getBlock().getBiome());
new_biome = new BiomeTag(to.getBlock().getBiome());
old_biome = new BiomeTag(NMSHandler.getInstance().getBiomeAt(from.getBlock()));
new_biome = new BiomeTag(NMSHandler.getInstance().getBiomeAt(to.getBlock()));
if (old_biome.identify().equals(new_biome.identify())) {
return;
}
Expand Down
Expand Up @@ -14,7 +14,9 @@
import net.md_5.bungee.api.chat.HoverEvent;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.persistence.PersistentDataContainer;
Expand Down Expand Up @@ -163,7 +165,11 @@ public boolean isCorrectMappingsCode() {

public abstract ProfileEditor getProfileEditor();

public abstract BiomeNMS getBiomeNMS(Biome biome);
public abstract BiomeNMS getBiomeNMS(World world, String name);

public BiomeNMS getBiomeAt(Block block) {
return NMSHandler.getInstance().getBiomeNMS(block.getWorld(), block.getBiome().name());
}

public abstract double[] getRecentTps();

Expand Down
@@ -1,17 +1,22 @@
package com.denizenscript.denizen.nms.abstracts;

import org.bukkit.block.Biome;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;

import java.util.ArrayList;
import java.util.List;

public abstract class BiomeNMS {

private final String name;
public String name;

public BiomeNMS(Biome biome) {
this.name = biome.name();
public World world;

public BiomeNMS(World world, String name) {
this.world = world;
this.name = CoreUtilities.toLowerCase(name);
}

public DownfallType getDownfallType() {
Expand Down Expand Up @@ -62,4 +67,6 @@ public enum DownfallType {
protected abstract boolean getDoesRain();

protected abstract boolean getDoesSnow();

public abstract void setTo(Block block);
}
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.objects;

import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
Expand All @@ -16,6 +17,8 @@
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.entity.EntityType;

Expand All @@ -29,13 +32,14 @@ public class BiomeTag implements ObjectTag, Adjustable, FlaggableObject {
// @base ElementTag
// @implements FlaggableObject
// @format
// The identity format for biomes is simply the biome name, as registered in Bukkit, for example: 'desert'.
// The identity format for biomes is a world name, then a comma, then the biome key. For example: 'hub,desert', or 'space,minecraft:desert'.
//
// @description
// A BiomeTag represents a world biome type.
// A BiomeTag represents a world biome type. Vanilla biomes are globally available, however some biomes are world-specific when added by datapacks.
//
// A list of all valid Bukkit biomes can found be at
// <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/block/Biome.html>
// A list of all vanilla biomes can be found at <@link url https://minecraft.fandom.com/wiki/Biome#Biome_IDs>.
//
// BiomeTags without a specific world will work as though they are in the server's default world.
//
// This object type is flaggable.
// Flags on this object type will be stored in the server saves file, under special sub-key "__biomes"
Expand All @@ -51,55 +55,59 @@ public static BiomeTag valueOf(String string) {
return valueOf(string, null);
}

/**
* Gets a Biome Object from a string form.
*
* @param string the string
*/
@Fetchable("b")
public static BiomeTag valueOf(String string, TagContext context) {

if (string.startsWith("b@")) {
string = string.substring(2);
}

for (Biome biome : Biome.values()) {
if (biome.name().equalsIgnoreCase(string)) {
return new BiomeTag(biome);
string = CoreUtilities.toLowerCase(string);
int comma = string.indexOf(',');
String worldName = null, biomeName = string;
if (comma != -1) {
worldName = string.substring(0, comma);
biomeName = string.substring(comma + 1);
}
World world = Bukkit.getWorlds().get(0);
if (worldName != null) {
WorldTag worldTag = WorldTag.valueOf(worldName, context);
if (worldTag == null || worldTag.getWorld() == null) {
return null;
}
world = worldTag.getWorld();
}

return null;
BiomeNMS biome = NMSHandler.getInstance().getBiomeNMS(world, biomeName);
if (biome == null) {
return null;
}
return new BiomeTag(biome);
}

/**
* Determines whether a string is a valid biome.
*
* @param arg the string
* @return true if matched, otherwise false
*/
public static boolean matches(String arg) {

if (arg.startsWith("b@")) {
arg = arg.substring(2);
}

for (Biome b : Biome.values()) {
if (b.name().equalsIgnoreCase(arg)) {
return true;
}
return true;
}

return false;
return valueOf(arg, CoreUtilities.noDebugContext) != null;
}

///////////////
// Constructors
/////////////

public BiomeTag(Biome biome) {
this.bukkitBiome = biome;
this.biome = NMSHandler.getInstance().getBiomeNMS(biome);
String key = biome.name();
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
if (biome.getKey().getNamespace().equals("minecraft")) {
key = biome.getKey().getKey();
}
else {
key = biome.getKey().toString();
}
}
this.biome = NMSHandler.getInstance().getBiomeNMS(Bukkit.getWorlds().get(0), key);
}

public BiomeTag(BiomeNMS biome) {
this.biome = biome;
}

/////////////////////
Expand All @@ -108,8 +116,6 @@ public BiomeTag(Biome biome) {

private BiomeNMS biome;

public Biome bukkitBiome;

public BiomeNMS getBiome() {
return biome;
}
Expand All @@ -133,7 +139,7 @@ public String getObjectType() {

@Override
public String identify() {
return "b@" + CoreUtilities.toLowerCase(biome.getName());
return "b@" + biome.world.getName() + "," + biome.getName();
}

@Override
Expand Down
@@ -1,6 +1,7 @@
package com.denizenscript.denizen.objects;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.nms.abstracts.BiomeNMS;
import com.denizenscript.denizen.objects.notable.NotableManager;
import com.denizenscript.denizen.objects.properties.material.MaterialDirectional;
import com.denizenscript.denizen.objects.properties.material.MaterialHalf;
Expand Down Expand Up @@ -452,7 +453,7 @@ public static BlockState getBlockStateSafe(Block block) {
}
}

public Biome getBiomeForTag(Attribute attribute) {
public BiomeNMS getBiomeForTag(Attribute attribute) {
NMSHandler.getChunkHelper().changeChunkServerThread(getWorld());
try {
if (getWorld() == null) {
Expand All @@ -467,7 +468,7 @@ public Biome getBiomeForTag(Attribute attribute) {
}
return null;
}
return super.getBlock().getBiome();
return NMSHandler.getInstance().getBiomeAt(super.getBlock());
}
finally {
NMSHandler.getChunkHelper().restoreServerThread(getWorld());
Expand Down Expand Up @@ -3088,7 +3089,7 @@ else if (PolygonTag.matches(attribute.getContext(1))) {
if (attribute.startsWith("formatted", 2)) {
Deprecations.locationBiomeFormattedTag.warn(attribute.context);
attribute.fulfill(1);
return new ElementTag(CoreUtilities.toLowerCase(object.getBiomeForTag(attribute).name()).replace('_', ' '));
return new ElementTag(CoreUtilities.toLowerCase(object.getBiomeForTag(attribute).getName()).replace('_', ' '));
}
return new BiomeTag(object.getBiomeForTag(attribute));
});
Expand Down Expand Up @@ -3656,7 +3657,7 @@ public void adjust(Mechanism mechanism) {
// <LocationTag.biome>
// -->
if (mechanism.matches("biome") && mechanism.requireObject(BiomeTag.class)) {
getBlock().setBiome(mechanism.valueAsType(BiomeTag.class).bukkitBiome);
mechanism.valueAsType(BiomeTag.class).getBiome().setTo(getBlock());
}

// <--[mechanism]
Expand Down
Expand Up @@ -122,7 +122,7 @@ public void adjust(Mechanism mechanism) {
// @tags
// <MaterialTag.mode>
// -->
if (mechanism.matches("mode") && mechanism.requireEnum(false, Comparator.Mode.values())) {
if (mechanism.matches("mode")) {
if (isComparator() && mechanism.requireEnum(false, Comparator.Mode.values())) {
getComparator().setMode(Comparator.Mode.valueOf(mechanism.getValue().asString().toUpperCase()));
}
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.craftbukkit.v1_14_R1.CraftServer;
import org.bukkit.craftbukkit.v1_14_R1.entity.CraftPlayer;
Expand Down Expand Up @@ -210,8 +211,13 @@ public ProfileEditor getProfileEditor() {
}

@Override
public BiomeNMS getBiomeNMS(Biome biome) {
return new BiomeNMSImpl(biome);
public BiomeNMS getBiomeNMS(World world, String name) {
for (Biome biome : Biome.values()) {
if (biome.name().equalsIgnoreCase(name)) {
return new BiomeNMSImpl(world, biome);
}
}
return null;
}

@Override
Expand Down
Expand Up @@ -3,7 +3,9 @@
import com.denizenscript.denizen.nms.abstracts.BiomeNMS;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import net.minecraft.server.v1_14_R1.*;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_14_R1.block.CraftBlock;
import org.bukkit.entity.EntityType;

Expand All @@ -15,8 +17,11 @@ public class BiomeNMSImpl extends BiomeNMS {

private final BiomeBase biomeBase;

public BiomeNMSImpl(Biome biome) {
super(biome);
public Biome bukkitBiome;

public BiomeNMSImpl(World world, Biome biome) {
super(world, biome.name());
bukkitBiome = biome;
this.biomeBase = CraftBlock.biomeToBiomeBase(biome);
}

Expand Down Expand Up @@ -88,4 +93,9 @@ private List<EntityType> getSpawnableEntities(EnumCreatureType creatureType) {
}
return entityTypes;
}

@Override
public void setTo(Block block) {
block.setBiome(bukkitBiome);
}
}
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.craftbukkit.v1_15_R1.CraftServer;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
Expand Down Expand Up @@ -210,8 +211,13 @@ public ProfileEditor getProfileEditor() {
}

@Override
public BiomeNMS getBiomeNMS(Biome biome) {
return new BiomeNMSImpl(biome);
public BiomeNMS getBiomeNMS(World world, String name) {
for (Biome biome : Biome.values()) {
if (biome.name().equalsIgnoreCase(name)) {
return new BiomeNMSImpl(world, biome);
}
}
return null;
}

@Override
Expand Down
Expand Up @@ -3,7 +3,9 @@
import com.denizenscript.denizen.nms.abstracts.BiomeNMS;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import net.minecraft.server.v1_15_R1.*;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_15_R1.block.CraftBlock;
import org.bukkit.entity.EntityType;

Expand All @@ -15,8 +17,11 @@ public class BiomeNMSImpl extends BiomeNMS {

public BiomeBase biomeBase;

public BiomeNMSImpl(Biome biome) {
super(biome);
public Biome bukkitBiome;

public BiomeNMSImpl(World world, Biome biome) {
super(world, biome.name());
bukkitBiome = biome;
this.biomeBase = CraftBlock.biomeToBiomeBase(biome);
}

Expand Down Expand Up @@ -88,4 +93,9 @@ private List<EntityType> getSpawnableEntities(EnumCreatureType creatureType) {
}
return entityTypes;
}

@Override
public void setTo(Block block) {
block.setBiome(bukkitBiome);
}
}

0 comments on commit 6163cc0

Please sign in to comment.