Skip to content

Commit

Permalink
in_water_duration and drowned_conversion_duration
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 11, 2021
1 parent df829b8 commit fb10bd7
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 3 deletions.
Expand Up @@ -25,6 +25,8 @@ public class PlayerBreaksBlockScriptEvent extends BukkitScriptEvent implements L
//
// @Regex ^on player breaks [^\s]+$
//
// @Synonyms player mines block,player mines ore,player digs block
//
// @Group Player
//
// @Location true
Expand Down
Expand Up @@ -381,4 +381,20 @@ public void setFireworkLifetime(Firework firework, int ticks) {
public int getFireworkLifetime(Firework firework) {
throw new UnsupportedOperationException();
}

public int getInWaterTime(Zombie zombie) {
throw new UnsupportedOperationException();
}

public void setInWaterTime(Zombie zombie, int ticks) {
throw new UnsupportedOperationException();
}

public int getDrownedConversionTicks(Zombie zombie) {
throw new UnsupportedOperationException();
}

public void setDrownedConversionTicks(Zombie zombie, int ticks) {
throw new UnsupportedOperationException();
}
}
Expand Up @@ -37,15 +37,14 @@ public static void registerMainProperties() {
PropertyParser.registerProperty(EntityArrowDamage.class, EntityTag.class);
PropertyParser.registerProperty(EntityAttributeBaseValues.class, EntityTag.class);
PropertyParser.registerProperty(EntityAttributeModifiers.class, EntityTag.class);
PropertyParser.registerProperty(EntityInvulnerable.class, EntityTag.class);
PropertyParser.registerProperty(EntityBoatType.class, EntityTag.class);
PropertyParser.registerProperty(EntityArmorPose.class, EntityTag.class);
PropertyParser.registerProperty(EntityArms.class, EntityTag.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_15)) {
PropertyParser.registerProperty(EntityAware.class, EntityTag.class);
}
PropertyParser.registerProperty(EntityBasePlate.class, EntityTag.class);
PropertyParser.registerProperty(EntityBeamTarget.class, EntityTag.class);
PropertyParser.registerProperty(EntityBoatType.class, EntityTag.class);
PropertyParser.registerProperty(EntityBodyArrows.class, EntityTag.class);
PropertyParser.registerProperty(EntityBoundingBox.class, EntityTag.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16)) {
Expand All @@ -65,6 +64,9 @@ public static void registerMainProperties() {
}
PropertyParser.registerProperty(EntityDirection.class, EntityTag.class);
PropertyParser.registerProperty(EntityDisabledSlots.class, EntityTag.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
PropertyParser.registerProperty(EntityDrownedConversionTime.class, EntityTag.class);
}
PropertyParser.registerProperty(EntityPotionEffects.class, EntityTag.class);
PropertyParser.registerProperty(EntityEquipment.class, EntityTag.class);
PropertyParser.registerProperty(EntityExplosionFire.class, EntityTag.class);
Expand Down Expand Up @@ -95,6 +97,10 @@ public static void registerMainProperties() {
PropertyParser.registerProperty(EntityImmune.class, EntityTag.class);
}
PropertyParser.registerProperty(EntityInventory.class, EntityTag.class);
PropertyParser.registerProperty(EntityInvulnerable.class, EntityTag.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
PropertyParser.registerProperty(EntityInWaterTime.class, EntityTag.class);
}
PropertyParser.registerProperty(EntityIsShowingBottom.class, EntityTag.class);
PropertyParser.registerProperty(EntityItem.class, EntityTag.class);
PropertyParser.registerProperty(EntityItemInHand.class, EntityTag.class);
Expand Down
@@ -0,0 +1,82 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.entity.Zombie;

public class EntityDrownedConversionTime implements Property {

public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag && ((EntityTag) entity).getBukkitEntity() instanceof Zombie;
}

public static EntityDrownedConversionTime getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityDrownedConversionTime((EntityTag) entity);
}
}

public static final String[] handledMechs = new String[] {
"drowned_conversion_duration"
};

private EntityDrownedConversionTime(EntityTag entity) {
this.entity = entity;
}

EntityTag entity;

@Override
public String getPropertyString() {
return new DurationTag((long) NMSHandler.getEntityHelper().getDrownedConversionTicks((Zombie) entity.getBukkitEntity())).identify();
}

@Override
public String getPropertyId() {
return "drowned_conversion_duration";
}

public static void registerTags() {

// <--[tag]
// @attribute <EntityTag.drowned_conversion_duration>
// @returns ElementTag(Boolean)
// @mechanism EntityTag.drowned_conversion_duration
// @group properties
// @description
// If the entity is a zombie mob, returns the duration of time the zombie will be converting to a Drowned for.
// If this value hits 0, the Zombie will become a Drowned.
// See also <@link tag EntityTag.in_water_duration>
// -->
PropertyParser.<EntityDrownedConversionTime, DurationTag>registerTag(DurationTag.class, "drowned_conversion_duration", (attribute, object) -> {
return new DurationTag((long) NMSHandler.getEntityHelper().getDrownedConversionTicks((Zombie) object.entity.getBukkitEntity()));
});
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object EntityTag
// @name drowned_conversion_duration
// @input ElementTag(Boolean)
// @description
// If the entity is a zombie mob, sets the duration of time the zombie will be converting to a Drowned for.
// If this value hits 0, the Zombie will become a Drowned.
// See also <@link mechanism EntityTag.in_water_duration>
// @tags
// <EntityTag.in_water_duration>
// -->
if (mechanism.matches("drowned_conversion_duration") && mechanism.requireObject(DurationTag.class)) {
NMSHandler.getEntityHelper().setDrownedConversionTicks((Zombie) entity.getBukkitEntity(), mechanism.valueAsType(DurationTag.class).getTicksAsInt());
}
}
}
@@ -0,0 +1,82 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.entity.Zombie;

public class EntityInWaterTime implements Property {

public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag && ((EntityTag) entity).getBukkitEntity() instanceof Zombie;
}

public static EntityInWaterTime getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityInWaterTime((EntityTag) entity);
}
}

public static final String[] handledMechs = new String[] {
"in_water_duration"
};

private EntityInWaterTime(EntityTag entity) {
this.entity = entity;
}

EntityTag entity;

@Override
public String getPropertyString() {
return new DurationTag((long) NMSHandler.getEntityHelper().getInWaterTime((Zombie) entity.getBukkitEntity())).identify();
}

@Override
public String getPropertyId() {
return "in_water_duration";
}

public static void registerTags() {

// <--[tag]
// @attribute <EntityTag.in_water_duration>
// @returns ElementTag(Boolean)
// @mechanism EntityTag.in_water_duration
// @group properties
// @description
// If the entity is a zombie mob, returns the duration of time the zombie has been in water for.
// If this value exceeds 600 ticks, the zombie will begin converted to a Drowned mob.
// See also <@link tag EntityTag.drowned_conversion_duration>
// -->
PropertyParser.<EntityInWaterTime, DurationTag>registerTag(DurationTag.class, "in_water_duration", (attribute, object) -> {
return new DurationTag((long) NMSHandler.getEntityHelper().getInWaterTime((Zombie) object.entity.getBukkitEntity()));
});
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object EntityTag
// @name in_water_duration
// @input ElementTag(Boolean)
// @description
// If the entity is a zombie mob, sets the duration of time the zombie has been in water for.
// If this value exceeds 600 ticks, the zombie will begin converted to a Drowned mob.
// See also <@link mechanism EntityTag.drowned_conversion_duration>
// @tags
// <EntityTag.in_water_duration>
// -->
if (mechanism.matches("in_water_duration") && mechanism.requireObject(DurationTag.class)) {
NMSHandler.getEntityHelper().setInWaterTime((Zombie) entity.getBukkitEntity(), mechanism.valueAsType(DurationTag.class).getTicksAsInt());
}
}
}
Expand Up @@ -25,6 +25,9 @@ public class ReflectionMappingsInfo {

public static String EnderMan_DATA_CREEPY = "bU";

public static String Zombie_inWaterTime = "cc";
public static String Zombie_conversionTime = "cd";

public static String Item_maxStackSize = "c";

public static String Level_isClientSide = "y";
Expand Down
Expand Up @@ -843,4 +843,50 @@ public void setFireworkLifetime(Firework firework, int ticks) {
public int getFireworkLifetime(Firework firework) {
return ((CraftFirework) firework).getHandle().lifetime;
}

public static final Field ZOMBIE_INWATERTIME = ReflectionHelper.getFields(net.minecraft.world.entity.monster.Zombie.class).get(ReflectionMappingsInfo.Zombie_inWaterTime);

@Override
public int getInWaterTime(Zombie zombie) {
try {
return ZOMBIE_INWATERTIME.getInt(((CraftZombie) zombie).getHandle());
}
catch (Throwable ex) {
Debug.echoError(ex);
return 0;
}
}

@Override
public void setInWaterTime(Zombie zombie, int ticks) {
try {
ZOMBIE_INWATERTIME.setInt(((CraftZombie) zombie).getHandle(), ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}

public static final Field ZOMBIE_CONVERSIONTIME = ReflectionHelper.getFields(net.minecraft.world.entity.monster.Zombie.class).get(ReflectionMappingsInfo.Zombie_conversionTime);

@Override
public int getDrownedConversionTicks(Zombie zombie) {
try {
return ZOMBIE_CONVERSIONTIME.getInt(((CraftZombie) zombie).getHandle());
}
catch (Throwable ex) {
Debug.echoError(ex);
return 0;
}
}

@Override
public void setDrownedConversionTicks(Zombie zombie, int ticks) {
try {
ZOMBIE_CONVERSIONTIME.setInt(((CraftZombie) zombie).getHandle(), ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
}
Expand Up @@ -2,7 +2,7 @@

public class ReflectionMappingsInfo {

// Contents gathered via https://minidigger.github.io/MiniMappingViewer/#/mojang/server/1.18
// Contents gathered via https://minidigger.github.io/MiniMappingViewer/#/mojang/server/1.18.1

// net.minecraft.advancements.AdvancementList
public static String AdvancementList_roots = "c";
Expand Down Expand Up @@ -36,6 +36,10 @@ public class ReflectionMappingsInfo {
// net.minecraft.world.entity.monster.EnderMan
public static String EnderMan_DATA_CREEPY = "bY";

// net.minecraft.world.entity.monster.Zombie
public static String Zombie_inWaterTime = "cf";
public static String Zombie_conversionTime = "cg";

// net.minecraft.world.item.Item
public static String Item_maxStackSize = "c";

Expand Down
Expand Up @@ -843,4 +843,50 @@ public void setFireworkLifetime(Firework firework, int ticks) {
public int getFireworkLifetime(Firework firework) {
return ((CraftFirework) firework).getHandle().lifetime;
}

public static final Field ZOMBIE_INWATERTIME = ReflectionHelper.getFields(net.minecraft.world.entity.monster.Zombie.class).get(ReflectionMappingsInfo.Zombie_inWaterTime);

@Override
public int getInWaterTime(Zombie zombie) {
try {
return ZOMBIE_INWATERTIME.getInt(((CraftZombie) zombie).getHandle());
}
catch (Throwable ex) {
Debug.echoError(ex);
return 0;
}
}

@Override
public void setInWaterTime(Zombie zombie, int ticks) {
try {
ZOMBIE_INWATERTIME.setInt(((CraftZombie) zombie).getHandle(), ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}

public static final Field ZOMBIE_CONVERSIONTIME = ReflectionHelper.getFields(net.minecraft.world.entity.monster.Zombie.class).get(ReflectionMappingsInfo.Zombie_conversionTime);

@Override
public int getDrownedConversionTicks(Zombie zombie) {
try {
return ZOMBIE_CONVERSIONTIME.getInt(((CraftZombie) zombie).getHandle());
}
catch (Throwable ex) {
Debug.echoError(ex);
return 0;
}
}

@Override
public void setDrownedConversionTicks(Zombie zombie, int ticks) {
try {
ZOMBIE_CONVERSIONTIME.setInt(((CraftZombie) zombie).getHandle(), ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
}

0 comments on commit fb10bd7

Please sign in to comment.