Skip to content

Commit

Permalink
v8.8.0
Browse files Browse the repository at this point in the history
Updated for 1.19

ItemStack
Added a lisp-like condition-restart system for deserialization material errors.
Changed the way how configs are accessed for a few options. This is necessary for external config handlers.
  • Loading branch information
CryptoMorin committed Jun 14, 2022
1 parent 6a1502f commit 467866f
Show file tree
Hide file tree
Showing 14 changed files with 664 additions and 239 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.cryptomorin</groupId>
<artifactId>XSeries</artifactId>
<version>8.7.1</version>
<version>8.8.0</version>

<name>XSeries</name>
<description>A set of utilities for Minecraft plugins</description>
Expand Down Expand Up @@ -63,7 +63,7 @@

<properties>
<!-- Latest version -->
<spigotVersion>1.18.2-R0.1-SNAPSHOT</spigotVersion>
<spigotVersion>1.19-R0.1-SNAPSHOT</spigotVersion>
</properties>

<dependencies>
Expand Down Expand Up @@ -240,6 +240,12 @@
</plugins>
</build>
</profile>
<profile>
<id>latest</id>
<properties>
<nms>19_R0</nms>
</properties>
</profile>
<profile>
<id>18</id>
<properties>
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/cryptomorin/xseries/NMSExtras.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
import java.lang.invoke.MethodType;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import java.util.*;

import static com.cryptomorin.xseries.ReflectionUtils.*;

Expand All @@ -47,7 +44,7 @@
* All the parameters are non-null.
*
* @author Crypto Morin
* @version 5.1.0
* @version 5.2.0
*/
public final class NMSExtras {
public static final MethodHandle EXP_PACKET;
Expand Down Expand Up @@ -116,8 +113,8 @@ public final class NMSExtras {

// https://wiki.vg/Protocol#Set_Experience
// exp - lvl - total exp
expPacket = lookup.findConstructor(getNMSClass("network.protocol.game", "PacketPlayOutExperience"), MethodType.methodType(void.class, float.class,
int.class, int.class));
expPacket = lookup.findConstructor(getNMSClass("network.protocol.game", "PacketPlayOutExperience"), MethodType.methodType(
void.class, float.class, int.class, int.class));
// Lightning
if (!supports(16)) {
entityPacket = lookup.findConstructor(getNMSClass("PacketPlayOutSpawnEntityWeather"), MethodType.methodType(void.class,
Expand All @@ -126,8 +123,14 @@ public final class NMSExtras {
vec3D = lookup.findConstructor(nmsVec3D, MethodType.methodType(void.class,
double.class, double.class, double.class));

entityPacket = lookup.findConstructor(getNMSClass("network.protocol.game", "PacketPlayOutSpawnEntity"), MethodType.methodType(void.class,
int.class, UUID.class, double.class, double.class, double.class, float.class, float.class, nmsEntityType, int.class, nmsVec3D));
List<Class<?>> spawnTypes = new ArrayList<>(Arrays.asList(
int.class, UUID.class,
double.class, double.class, double.class, float.class, float.class,
nmsEntityType, int.class, nmsVec3D)
);
if (ReflectionUtils.supports(19)) spawnTypes.add(double.class);
entityPacket = lookup.findConstructor(getNMSClass("network.protocol.game", "PacketPlayOutSpawnEntity"),
MethodType.methodType(void.class, spawnTypes));
}

worldHandle = lookup.findVirtual(getCraftClass("CraftWorld"), "getHandle", MethodType.methodType(
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/cryptomorin/xseries/XBiome.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* <a href="https://minecraft.fandom.com/wiki/Java_Edition_1.18">1.18 removed biomes issue.</a>
*
* @author Crypto Morin
* @version 6.0.0
* @version 6.1.0
* @see Biome
*/
public enum XBiome {
Expand All @@ -62,6 +62,8 @@ public enum XBiome {
OLD_GROWTH_SPRUCE_TAIGA("GIANT_SPRUCE_TAIGA", "MUTATED_REDWOOD_TAIGA", "MEGA_SPRUCE_TAIGA"),
WINDSWEPT_SAVANNA("SHATTERED_SAVANNA", "MUTATED_SAVANNA", "SAVANNA_MOUNTAINS"),
MEADOW,
MANGROVE_SWAMP,
DEEP_DARK,
GROVE,
SNOWY_SLOPES,
FROZEN_PEAKS,
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/cryptomorin/xseries/XEnchantment.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* Enchanting: https://minecraft.gamepedia.com/Enchanting
*
* @author Crypto Morin
* @version 2.2.0
* @version 2.3.0
* @see Enchantment
*/
public enum XEnchantment {
Expand All @@ -68,7 +68,6 @@ public enum XEnchantment {
FIRE_ASPECT(true, "FIRE", "MELEE_FIRE", "MELEE_FLAME", "FA"),
FROST_WALKER(true, "FROST", "WALKER"),
IMPALING(true, "IMPALE", "OCEAN_DAMAGE", "OCEAN_DMG"),
SOUL_SPEED(true, "SPEED_SOUL", "SOUL_RUNNER"),
KNOCKBACK(true, "K_BACK", "KB"),
LOOT_BONUS_BLOCKS("FORTUNE", "BLOCKS_LOOT_BONUS", "FORT", "LBB"),
LOOT_BONUS_MOBS("LOOTING", "MOB_LOOT", "MOBS_LOOT_BONUS", "LBM"),
Expand All @@ -87,7 +86,9 @@ public enum XEnchantment {
QUICK_CHARGE(true, "QUICKCHARGE", "QUICK_DRAW", "FAST_CHARGE", "FAST_DRAW"),
RIPTIDE(true, "RIP", "TIDE", "LAUNCH"),
SILK_TOUCH(true, "SOFT_TOUCH", "ST"),
SOUL_SPEED(true, "SPEED_SOUL", "SOUL_RUNNER"),
SWEEPING_EDGE("SWEEPING", "SWEEPING_EDGE", "SWEEP_EDGE"),
SWIFT_SNEAK(true, "SNEAK_SWIFT"),
THORNS(true, "HIGHCRIT", "THORN", "HIGHERCRIT", "T"),
VANISHING_CURSE(true, "VANISHING_CURSE", "VANISH_CURSE", "VANISHING", "VANISH"),
WATER_WORKER("AQUA_AFFINITY", "WATER_WORKER", "AQUA_AFFINITY", "WATER_MINE", "WW");
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/cryptomorin/xseries/XEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
* Entity: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Entity.html
*
* @author Crypto Morin
* @version 4.0.0
* @version 4.0.1
* @see XMaterial
* @see XItemStack
* @see XPotion
Expand Down Expand Up @@ -135,13 +135,12 @@ public static Entity edit(@Nonnull Entity entity, @Nonnull ConfigurationSection
if (config.isSet("silent")) entity.setSilent(config.getBoolean("silent"));
entity.setFireTicks(config.getInt("fire-ticks"));
entity.setFallDistance(config.getInt("fall-distance"));
entity.setInvulnerable(config.getBoolean("invulnerable"));
if (config.isSet("invulnerable")) entity.setInvulnerable(config.getBoolean("invulnerable"));

int live = config.getInt("ticks-lived");
if (live > 0) entity.setTicksLived(live);

int portalCooldown = config.getInt("portal-cooldown", -1);
if (portalCooldown != -1) entity.setPortalCooldown(portalCooldown);
if (config.isSet("portal-cooldown")) entity.setPortalCooldown(config.getInt("portal-cooldown", -1));
// We don't need damage cause.

if (XMaterial.supports(13)) {
Expand Down Expand Up @@ -181,8 +180,8 @@ public static Entity edit(@Nonnull Entity entity, @Nonnull ConfigurationSection

if (entity instanceof LivingEntity) {
LivingEntity living = (LivingEntity) entity;
double hp = config.getDouble("health", -1);
if (hp > -1) {
if (config.isSet("health")) {
double hp = config.getDouble("health");
living.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(hp);
living.setHealth(hp);
}
Expand Down
124 changes: 115 additions & 9 deletions src/main/java/com/cryptomorin/xseries/XItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

Expand All @@ -74,7 +75,7 @@
* ItemStack: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/ItemStack.html
*
* @author Crypto Morin
* @version 7.1.0
* @version 7.2.0
* @see XMaterial
* @see XPotion
* @see SkullUtils
Expand Down Expand Up @@ -366,7 +367,7 @@ public static Map<String, Object> serialize(@Nonnull ItemStack item) {
*/
@Nonnull
public static ItemStack deserialize(@Nonnull ConfigurationSection config) {
return edit(new ItemStack(Material.AIR), config, Function.identity());
return edit(new ItemStack(Material.AIR), config, Function.identity(), null);
}

private static List<String> splitNewLine(String str) {
Expand Down Expand Up @@ -397,19 +398,45 @@ private static List<String> splitNewLine(String str) {
return list;
}

@Nonnull
public static ItemStack deserialize(@Nonnull ConfigurationSection config,
@Nonnull Function<String, String> translator) {
return deserialize(config, translator, null);
}

/**
* Deserialize an ItemStack from the config.
*
* @param config the config section to deserialize the ItemStack object from.
*
* @return an edited ItemStack.
* @since 7.0.0
* @since 7.2.0
*/
@Nonnull
public static ItemStack deserialize(@Nonnull ConfigurationSection config,
@Nonnull Function<String, String> translator,
@Nullable Consumer<Exception> restart) {
return edit(new ItemStack(Material.AIR), config, translator, restart);
}


/**
* Deserialize an ItemStack from a {@code Map}.
*
* @param serializedItem the map holding the item configurations to deserialize
* the ItemStack object from.
* @param translator the translator to use for translating the item's name.
*
* @return a deserialized ItemStack.
*/
@Nonnull
public static ItemStack deserialize(@Nonnull ConfigurationSection config, @Nonnull Function<String, String> translator) {
return edit(new ItemStack(Material.AIR), config, translator);
public static ItemStack deserialize(@Nonnull Map<String, Object> serializedItem, @Nonnull Function<String, String> translator) {
Objects.requireNonNull(serializedItem, "serializedItem cannot be null.");
Objects.requireNonNull(translator, "translator cannot be null.");
return deserialize(mapToConfigSection(serializedItem), translator);
}


/**
* Deserialize an ItemStack from the config.
*
Expand All @@ -420,16 +447,45 @@ public static ItemStack deserialize(@Nonnull ConfigurationSection config, @Nonnu
*/
@SuppressWarnings("deprecation")
@Nonnull
public static ItemStack edit(@Nonnull ItemStack item, @Nonnull ConfigurationSection config, @Nonnull Function<String, String> translator) {
public static ItemStack edit(@Nonnull ItemStack item,
@Nonnull ConfigurationSection config,
@Nonnull Function<String, String> translator,
@Nullable Consumer<Exception> restart) {
Objects.requireNonNull(item, "Cannot operate on null ItemStack, considering using an AIR ItemStack instead");
Objects.requireNonNull(config, "Cannot deserialize item to a null configuration section.");
Objects.requireNonNull(translator, "Translator function cannot be null");

// Material
String materialName = config.getString("material");
Optional<XMaterial> material = Strings.isNullOrEmpty(materialName) ?
Optional.empty() : XMaterial.matchXMaterial(materialName);
if (material.isPresent()) material.get().setType(item);
if (!Strings.isNullOrEmpty(materialName)) {
Optional<XMaterial> materialOpt = XMaterial.matchXMaterial(materialName);
XMaterial material;
if (materialOpt.isPresent()) material = materialOpt.get();
else {
UnknownMaterialCondition unknownMaterialCondition = new UnknownMaterialCondition(materialName);
restart.accept(unknownMaterialCondition);

if (unknownMaterialCondition.hasSolution()) material = unknownMaterialCondition.solution;
else throw unknownMaterialCondition;
}

if (!material.isSupported()) {
UnAcceptableMaterialCondition unsupportedMaterialCondition = new UnAcceptableMaterialCondition(material, UnAcceptableMaterialCondition.Reason.UNSUPPORTED);
restart.accept(unsupportedMaterialCondition);

if (unsupportedMaterialCondition.hasSolution()) material = unsupportedMaterialCondition.solution;
else throw unsupportedMaterialCondition;
}
if (XTag.INVENTORY_NOT_DISPLAYABLE.isTagged(material)) {
UnAcceptableMaterialCondition unsupportedMaterialCondition = new UnAcceptableMaterialCondition(material, UnAcceptableMaterialCondition.Reason.NOT_DISPLAYABLE);
restart.accept(unsupportedMaterialCondition);

if (unsupportedMaterialCondition.hasSolution()) material = unsupportedMaterialCondition.solution;
else throw unsupportedMaterialCondition;
}

material.setType(item);
}

// Amount
int amount = config.getInt("amount");
Expand Down Expand Up @@ -1151,4 +1207,54 @@ public static int firstPartialOrEmpty(@Nonnull Inventory inventory, @Nullable It
}
return -1;
}

public static class MaterialCondition extends RuntimeException {
protected XMaterial solution;

public MaterialCondition(String message) {
super(message);
}

public void setSolution(XMaterial solution) {
this.solution = solution;
}

public boolean hasSolution() {
return this.solution != null;
}
}

public static final class UnknownMaterialCondition extends MaterialCondition {
private final String material;

public UnknownMaterialCondition(String material) {
super("Unknown material: " + material);
this.material = material;
}

public String getMaterial() {
return material;
}
}

public static final class UnAcceptableMaterialCondition extends MaterialCondition {
private final XMaterial material;
private final Reason reason;

public UnAcceptableMaterialCondition(XMaterial material, Reason reason) {
super("Unacceptable material: " + material.name() + " (" + reason.name() + ')');
this.material = material;
this.reason = reason;
}

public Reason getReason() {
return reason;
}

public XMaterial getMaterial() {
return material;
}

public enum Reason {UNSUPPORTED, NOT_DISPLAYABLE}
}
}
Loading

0 comments on commit 467866f

Please sign in to comment.