Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakllp committed Aug 3, 2024
2 parents 0a3c650 + 03849a6 commit a0c2d23
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ public enum MyPetType {
.v("1.7.10", 62)
.v("1.13", "magma_cube")
.search()),
Mooshroom("MUSHROOM_COW", "1.7.10", MyMooshroom.class, new Compat<>()
Mooshroom((String) new Compat<>().v("1.7.10","MUSHROOM_COW")
.v("1.20.6","MOOSHROOM").search().get(),
"1.7.10", MyMooshroom.class, new Compat<>()
.v("1.7.10", 96)
.v("1.13", "mooshroom")
.search()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
import de.Keyle.MyPet.api.skill.SkillName;
import de.Keyle.MyPet.api.skill.UpgradeComputer;
import de.Keyle.MyPet.api.skill.skilltree.Skill;
import de.Keyle.MyPet.api.util.NBTStorage;
import de.Keyle.MyPet.api.util.Scheduler;

@SkillName(value = "Behavior", translationNode = "Name.Skill.Behavior")
public interface Behavior extends Skill, Scheduler, ActiveSkill {
public interface Behavior extends Skill, Scheduler, ActiveSkill, NBTStorage {
enum BehaviorMode {
Normal, Friendly, Aggressive, Raid, Farm, Duel
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import de.Keyle.MyPet.api.skill.UpgradeComputer;
import de.Keyle.MyPet.api.skill.skills.Behavior;
import de.Keyle.MyPet.api.util.locale.Translation;
import de.keyle.knbt.*;
import org.bukkit.ChatColor;

import java.util.*;
Expand Down Expand Up @@ -207,4 +208,38 @@ public String toString() {
", selectedBehavior=" + selectedBehavior +
'}';
}

@Override
public TagCompound save() {
TagCompound nbtTagCompound = new TagCompound();
nbtTagCompound.getCompoundData().put("selectedBehavior", new TagString(this.selectedBehavior.name()));
return nbtTagCompound;
}

@Override
public void load(TagCompound tagCompound) {
if (tagCompound.containsKey("selectedBehavior")) {
String behaviorString = tagCompound.getAs("selectedBehavior", TagString.class).getStringData();
switch (behaviorString) {
case "Friendly":
setBehavior(Friendly);
break;
case "Aggressive":
setBehavior(Aggressive);
break;
case "Raid":
setBehavior(Raid);
break;
case "Farm":
setBehavior(Farm);
break;
case "Duel":
setBehavior(Duel);
break;
default:
setBehavior(Normal);
break;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import de.Keyle.MyPet.api.util.ReflectionUtil;
import de.Keyle.MyPet.api.util.inventory.material.ItemDatabase;
import de.Keyle.MyPet.compat.v1_20_R3.entity.EntityMyAquaticPet;
import de.Keyle.MyPet.compat.v1_20_R3.entity.ai.movement.MyPetAquaticMoveControl;
import de.Keyle.MyPet.compat.v1_20_R3.util.inventory.ItemStackNBTConverter;
import de.keyle.knbt.TagCompound;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
Expand Down
2 changes: 1 addition & 1 deletion modules/v1_20_R4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<dependency>
<groupId>com.mojang</groupId>
<artifactId>datafixerupper</artifactId>
<version>4.0.26</version>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.mojang</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package de.Keyle.MyPet.compat.v1_20_R4;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import de.Keyle.MyPet.MyPetApi;
import de.Keyle.MyPet.api.entity.MyPetMinecraftEntity;
import de.Keyle.MyPet.api.player.MyPetPlayer;
Expand All @@ -39,6 +38,7 @@
import net.minecraft.commands.arguments.ParticleArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;
Expand Down Expand Up @@ -91,6 +91,7 @@ public class PlatformHelper extends de.Keyle.MyPet.api.PlatformHelper {
private static final StackWalker leWalker = StackWalker.getInstance(Collections.singleton(StackWalker.Option.RETAIN_CLASS_REFERENCE), 4);
public static final Field dragonPartsField = ReflectionUtil.getField(ServerLevel.class, "ac"); //Mojang Field: dragonParts
private static final RegistryAccess REGISTRY_ACCESS = CraftRegistry.getMinecraftRegistry();
private static Method readParticleMethod = ReflectionUtil.getMethod(ParticleArgument.class,"a", StringReader.class, ParticleType.class, HolderLookup.Provider.class);


/**
Expand All @@ -115,8 +116,9 @@ public void playParticleEffect(Location location, String effectName, float offse

if (effect.codec().codec() != null && data != null) {
try {
particle = ParticleArgument.readParticle(new StringReader(" " + data.get().toString()), REGISTRY_ACCESS);
} catch (CommandSyntaxException e) {
String nbt_string = parseNBTForEffect(effectName);
particle = (ParticleOptions) readParticleMethod.invoke(null, new StringReader("{"+nbt_string+":\""+data.get().toString()+"\"}"), effect, REGISTRY_ACCESS);
} catch (Exception e) {
e.printStackTrace();
}
} else if (effect instanceof SimpleParticleType) {
Expand Down Expand Up @@ -159,8 +161,9 @@ public void playParticleEffect(Player player, Location location, String effectNa

if (effect.codec().codec() != null && data != null) {
try {
particle = ParticleArgument.readParticle(new StringReader(" " + data.get().toString()), REGISTRY_ACCESS);
} catch (CommandSyntaxException e) {
String nbt_string = parseNBTForEffect(effectName);
particle = (ParticleOptions) readParticleMethod.invoke(null, new StringReader("{"+nbt_string+":\""+data.get().toString()+"\"}"), effect, REGISTRY_ACCESS);
} catch (Exception e) {
e.printStackTrace();
}
} else if (effect instanceof SimpleParticleType) {
Expand Down Expand Up @@ -430,4 +433,13 @@ public boolean gameruleDoDeathMessages(LivingEntity entity) {
public boolean doStackWalking(Class leClass, int oldDepth) {
return leWalker.walk(s -> s.limit(oldDepth+1).map(StackWalker.StackFrame::getDeclaringClass).anyMatch(leClass::equals));
}

private String parseNBTForEffect(String effectName) {
return switch (effectName) {
case "item" -> "item";
case "block", "block_marker", "falling_dust", "dust_pillar" -> "block_state";
case "entity_effect" -> "color";
default -> "";
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static Tag save(ItemStack itemStack, CompoundTag tag) {
return itemStack.save(registryAccess, tag);
}

public static ItemStack parseItemStack(Tag tag) {
return ItemStack.parse(registryAccess, tag).orElseThrow();
public static ItemStack parseItemStack(CompoundTag tag) {
return ItemStack.parseOptional(registryAccess, tag);
}

public static String serializeComponent(Component cm) {
Expand Down
2 changes: 1 addition & 1 deletion modules/v1_21_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<dependency>
<groupId>com.mojang</groupId>
<artifactId>datafixerupper</artifactId>
<version>4.0.26</version>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.mojang</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package de.Keyle.MyPet.compat.v1_21_R1;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import de.Keyle.MyPet.MyPetApi;
import de.Keyle.MyPet.api.entity.MyPetMinecraftEntity;
import de.Keyle.MyPet.api.player.MyPetPlayer;
Expand All @@ -39,6 +38,7 @@
import net.minecraft.commands.arguments.ParticleArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;
Expand Down Expand Up @@ -91,6 +91,7 @@ public class PlatformHelper extends de.Keyle.MyPet.api.PlatformHelper {
private static final StackWalker leWalker = StackWalker.getInstance(Collections.singleton(StackWalker.Option.RETAIN_CLASS_REFERENCE), 4);
public static final Field dragonPartsField = ReflectionUtil.getField(ServerLevel.class, "ac"); //Mojang Field: dragonParts
private static final RegistryAccess REGISTRY_ACCESS = CraftRegistry.getMinecraftRegistry();
private static Method readParticleMethod = ReflectionUtil.getMethod(ParticleArgument.class,"a", StringReader.class, ParticleType.class, HolderLookup.Provider.class);


/**
Expand All @@ -105,7 +106,7 @@ public class PlatformHelper extends de.Keyle.MyPet.api.PlatformHelper {
*/
@Override
public void playParticleEffect(Location location, String effectName, float offsetX, float offsetY, float offsetZ, float speed, int count, int radius, de.Keyle.MyPet.api.compat.Compat<Object> data) {
ParticleType effect = BuiltInRegistries.PARTICLE_TYPE.get(ResourceLocation.tryParse(effectName));
ParticleType<?> effect = BuiltInRegistries.PARTICLE_TYPE.get(ResourceLocation.tryParse(effectName));

Validate.notNull(location, "Location cannot be null");
Validate.notNull(effect, "Effect cannot be null");
Expand All @@ -115,8 +116,9 @@ public void playParticleEffect(Location location, String effectName, float offse

if (effect.codec().codec() != null && data != null) {
try {
particle = ParticleArgument.readParticle(new StringReader(" " + data.get().toString()), REGISTRY_ACCESS);
} catch (CommandSyntaxException e) {
String nbt_string = parseNBTForEffect(effectName);
particle = (ParticleOptions) readParticleMethod.invoke(null, new StringReader("{"+nbt_string+":\""+data.get().toString()+"\"}"), effect, REGISTRY_ACCESS);
} catch (Exception e) {
e.printStackTrace();
}
} else if (effect instanceof SimpleParticleType) {
Expand Down Expand Up @@ -149,7 +151,7 @@ public void playParticleEffect(Location location, String effectName, float offse
*/
@Override
public void playParticleEffect(Player player, Location location, String effectName, float offsetX, float offsetY, float offsetZ, float speed, int count, int radius, de.Keyle.MyPet.api.compat.Compat<Object> data) {
ParticleType effect = BuiltInRegistries.PARTICLE_TYPE.get(ResourceLocation.tryParse(effectName));
ParticleType<?> effect = BuiltInRegistries.PARTICLE_TYPE.get(ResourceLocation.tryParse(effectName));

Validate.notNull(location, "Location cannot be null");
Validate.notNull(effect, "Effect cannot be null");
Expand All @@ -159,8 +161,9 @@ public void playParticleEffect(Player player, Location location, String effectNa

if (effect.codec().codec() != null && data != null) {
try {
particle = ParticleArgument.readParticle(new StringReader(" " + data.get().toString()), REGISTRY_ACCESS);
} catch (CommandSyntaxException e) {
String nbt_string = parseNBTForEffect(effectName);
particle = (ParticleOptions) readParticleMethod.invoke(null, new StringReader("{"+nbt_string+":\""+data.get().toString()+"\"}"), effect, REGISTRY_ACCESS);
} catch (Exception e) {
e.printStackTrace();
}
} else if (effect instanceof SimpleParticleType) {
Expand Down Expand Up @@ -432,4 +435,13 @@ public boolean gameruleDoDeathMessages(LivingEntity entity) {
public boolean doStackWalking(Class leClass, int oldDepth) {
return leWalker.walk(s -> s.limit(oldDepth+1).map(StackWalker.StackFrame::getDeclaringClass).anyMatch(leClass::equals));
}

private String parseNBTForEffect(String effectName) {
return switch (effectName) {
case "item" -> "item";
case "block", "block_marker", "falling_dust", "dust_pillar" -> "block_state";
case "entity_effect" -> "color";
default -> "";
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static Tag save(ItemStack itemStack, CompoundTag tag) {
return itemStack.save(registryAccess, tag);
}

public static ItemStack parseItemStack(Tag tag) {
return ItemStack.parse(registryAccess, tag).orElseThrow();
public static ItemStack parseItemStack(CompoundTag tag) {
return ItemStack.parseOptional(registryAccess, tag);
}

public static String serializeComponent(Component cm) {
Expand Down

0 comments on commit a0c2d23

Please sign in to comment.