Skip to content

Commit

Permalink
Fixed Grow-Up-Item for a few Pets - Fixed a Java16 specific bug - RC2
Browse files Browse the repository at this point in the history
Fixed:
- Dying pets won't throw errors anymore/kick players
- Fixed the grow-up-item behaviour for some pets

Bump to RC2
  • Loading branch information
Jakllp committed Sep 2, 2021
1 parent 0df4728 commit 474496f
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 38 deletions.
44 changes: 44 additions & 0 deletions modules/API/src/main/java/de/Keyle/MyPet/api/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ public static class Backpack {
}

public static class MyPet {
public static class Axolotl {
public static ConfigItem GROW_UP_ITEM;
}

public static class Bat {

Expand All @@ -203,12 +206,18 @@ public static class Bat {
public static class Bee {

public static boolean CAN_GLIDE = true;
public static ConfigItem GROW_UP_ITEM;
}

public static class Blaze {

public static boolean CAN_GLIDE = true;
}

public static class Cat {

public static ConfigItem GROW_UP_ITEM;
}

public static class Chicken {

Expand All @@ -227,7 +236,17 @@ public static class Donkey {

public static ConfigItem GROW_UP_ITEM;
}

public static class Drowned {

public static ConfigItem GROW_UP_ITEM;
}

public static class Fox {

public static ConfigItem GROW_UP_ITEM;
}

public static class EnderDragon {

public static boolean CAN_GLIDE = true;
Expand All @@ -253,6 +272,11 @@ public static class Horse {

public static ConfigItem GROW_UP_ITEM;
}

public static class Husk {

public static ConfigItem GROW_UP_ITEM;
}

public static class IronGolem {

Expand Down Expand Up @@ -280,6 +304,11 @@ public static class Ocelot {
public static ConfigItem GROW_UP_ITEM;
}

public static class Panda {

public static ConfigItem GROW_UP_ITEM;
}

public static class Parrot {

public static boolean CAN_GLIDE = true;
Expand Down Expand Up @@ -310,6 +339,11 @@ public static class PigZombie {
public static ConfigItem GROW_UP_ITEM;
}

public static class PolarBear {

public static ConfigItem GROW_UP_ITEM;
}

public static class Rabbit {

public static ConfigItem GROW_UP_ITEM;
Expand All @@ -336,6 +370,16 @@ public static class Strider {

public static ConfigItem GROW_UP_ITEM;
}

public static class TraderLlama {

public static ConfigItem GROW_UP_ITEM;
}

public static class Turtle {

public static ConfigItem GROW_UP_ITEM;
}

public static class Vex {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.bukkit.Bukkit;

public class ReflectionUtil {

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -119,14 +121,17 @@ public static boolean setFinalFieldValue(String fieldName, Object target, Object
public static boolean setFinalFieldValue(@NonNull Field field, Object target, Object value) {
try {
field.setAccessible(true);

Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);


if(Integer.parseInt(System.getProperty("java.version").split("\\.")[0]) < 16) { //Java-Version-Check
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}

field.set(target, value);
return true;
} catch (Throwable ignored) {
} catch (Exception ignored) {
ignored.printStackTrace();
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/MyPet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<groupId>de.keyle</groupId>
<artifactId>mypet</artifactId>
<version>3.12-RC1</version>
<version>3.12-RC2</version>
<packaging>jar</packaging>
<name>MyPet</name>
<url>https://www.spigotmc.org/resources/mypet.12725/</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,38 @@

package de.Keyle.MyPet.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
import org.bukkit.event.entity.CreatureSpawnEvent;

import com.google.common.collect.Lists;

import de.Keyle.MyPet.MyPetApi;
import de.Keyle.MyPet.api.Configuration.*;
import de.Keyle.MyPet.api.Configuration.Entity;
import de.Keyle.MyPet.api.Configuration.HungerSystem;
import de.Keyle.MyPet.api.Configuration.LevelSystem;
import de.Keyle.MyPet.api.Configuration.Log;
import de.Keyle.MyPet.api.Configuration.Misc;
import de.Keyle.MyPet.api.Configuration.MyPet;
import de.Keyle.MyPet.api.Configuration.Name;
import de.Keyle.MyPet.api.Configuration.Permissions;
import de.Keyle.MyPet.api.Configuration.Repository;
import de.Keyle.MyPet.api.Configuration.Respawn;
import de.Keyle.MyPet.api.Configuration.Skilltree;
import de.Keyle.MyPet.api.Configuration.Update;
import de.Keyle.MyPet.api.entity.DefaultInfo;
import de.Keyle.MyPet.api.entity.MyPetType;
import de.Keyle.MyPet.api.skill.experience.MonsterExperience;
Expand All @@ -31,18 +60,6 @@
import de.Keyle.MyPet.api.util.NameFilter;
import de.Keyle.MyPet.api.util.configuration.settings.Settings;
import de.Keyle.MyPet.util.sentry.SentryErrorReporter;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
import org.bukkit.event.entity.CreatureSpawnEvent;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class ConfigurationLoader {

Expand Down Expand Up @@ -521,29 +538,39 @@ public static void loadCompatConfiguration() {
MyPetApi.getLogger().warning("There was an error while loading pet-config.yml");
}
}

MyPet.Axolotl.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Axolotl.GrowUpItem", "experience_bottle"));
MyPet.Bee.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Bee.GrowUpItem", "experience_bottle"));
MyPet.Cat.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Cat.GrowUpItem", "experience_bottle"));
MyPet.Chicken.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Chicken.GrowUpItem", "experience_bottle"));
MyPet.Cow.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Cow.GrowUpItem", "experience_bottle"));
MyPet.Donkey.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Donkey.GrowUpItem", "experience_bottle"));
MyPet.Drowned.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Drowned.GrowUpItem", "experience_bottle"));
MyPet.Fox.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Fox.GrowUpItem", "experience_bottle"));
MyPet.Goat.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Goat.GrowUpItem", "experience_bottle"));
MyPet.Hoglin.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Hoglin.GrowUpItem", "experience_bottle"));
MyPet.Horse.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Horse.GrowUpItem", "" + "bread"));
MyPet.Husk.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Husk.GrowUpItem", "experience_bottle"));
MyPet.Llama.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Llama.GrowUpItem", "experience_bottle"));
MyPet.Mooshroom.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Mooshroom.GrowUpItem", "experience_bottle"));
MyPet.Mule.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Mule.GrowUpItem", "experience_bottle"));
MyPet.Ocelot.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Ocelot.GrowUpItem", "experience_bottle"));
MyPet.Panda.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Panda.GrowUpItem", "experience_bottle"));
MyPet.Pig.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Pig.GrowUpItem", "experience_bottle"));
MyPet.Piglin.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Piglin.GrowUpItem", "experience_bottle"));
MyPet.PiglinBrute.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.PiglinBrute.GrowUpItem", "experience_bottle"));
MyPet.PigZombie.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.PigZombie.GrowUpItem", "experience_bottle"));
MyPet.PolarBear.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.PolarBear.GrowUpItem", "experience_bottle"));
MyPet.Rabbit.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Rabbit.GrowUpItem", "experience_bottle"));
MyPet.Sheep.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Sheep.GrowUpItem", "experience_bottle"));
MyPet.SkeletonHorse.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.SkeletonHorse.GrowUpItem", "experience_bottle"));
MyPet.Strider.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Strider.GrowUpItem", "experience_bottle"));
MyPet.TraderLlama.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.TraderLlama.GrowUpItem", "experience_bottle"));
MyPet.Turtle.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Turtle.GrowUpItem", "experience_bottle"));
MyPet.Villager.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Villager.GrowUpItem", "experience_bottle"));
MyPet.Wolf.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Wolf.GrowUpItem", "experience_bottle"));
MyPet.Zoglin.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Zoglin.GrowUpItem", "experience_bottle"));
MyPet.Zombie.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Zombie.GrowUpItem", "experience_bottle"));
MyPet.PigZombie.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.PigZombie.GrowUpItem", "experience_bottle"));
MyPet.Rabbit.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Rabbit.GrowUpItem", "experience_bottle"));
MyPet.ZombieHorse.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.ZombieHorse.GrowUpItem", "experience_bottle"));
MyPet.SkeletonHorse.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.SkeletonHorse.GrowUpItem", "experience_bottle"));
MyPet.Mule.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Mule.GrowUpItem", "experience_bottle"));
MyPet.Donkey.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Donkey.GrowUpItem", "experience_bottle"));
MyPet.Zoglin.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Zoglin.GrowUpItem", "experience_bottle"));
MyPet.Hoglin.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Hoglin.GrowUpItem", "experience_bottle"));
MyPet.Strider.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Strider.GrowUpItem", "experience_bottle"));
MyPet.Piglin.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Piglin.GrowUpItem", "experience_bottle"));
MyPet.PiglinBrute.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.PiglinBrute.GrowUpItem", "experience_bottle"));
MyPet.ZombifiedPiglin.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.ZombifiedPiglin.GrowUpItem", "experience_bottle"));

for (MyPetType petType : MyPetType.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ public EntityMyPet(Level world, MyPet myPet) {
}

protected void replaceCraftAttributes() {
//TODO check this
Field craftAttributesField = ReflectionUtil.getField(LivingEntity.class, "craftAttributes");
CraftAttributeMap craftAttributes = new CraftAttributeMap(this.getAttributes());
ReflectionUtil.setFinalFieldValue(craftAttributesField, this, craftAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@

import java.util.HashMap;

import org.bukkit.Bukkit;

import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.GlowSquid;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ambient.Bat;
import net.minecraft.world.entity.animal.Bee;
Expand Down Expand Up @@ -103,7 +102,7 @@

public class MyAttributeDefaults {

private static final FastMap<EntityType<?>, AttributeSupplier> defaultAttribute = new FastMap<>();
private static final FastMap<EntityType<? extends LivingEntity>, AttributeSupplier> defaultAttribute = new FastMap<>();

static {
defaultAttribute
Expand Down Expand Up @@ -189,14 +188,14 @@ public static AttributeSupplier getAttribute(EntityType<?> types) {
return defaultAttribute.get(types);
}

public static void registerCustomEntityType(EntityType<?> customType, EntityType<?> rootType) {
public static void registerCustomEntityType(EntityType<? extends LivingEntity> customType, EntityType<? extends LivingEntity> rootType) {
defaultAttribute.put(customType, getAttribute(rootType));
}

static class FastMap<K, V> extends HashMap<K, V> {

public FastMap<K, V> putFast(K key, AttributeSupplier attributeSupplier) {
put(key, (V) attributeSupplier);
public FastMap<K, V> putFast(K key, V value) {
put(key, value);
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@

import org.bukkit.block.data.Ageable;

import de.Keyle.MyPet.api.Configuration;
import de.Keyle.MyPet.api.entity.EntitySize;
import de.Keyle.MyPet.api.entity.MyPet;
import de.Keyle.MyPet.api.entity.types.MyAxolotl;
import de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

@EntitySize(width = 0.7F, height = 1.3F)
Expand All @@ -56,6 +61,27 @@ protected String getHurtSound() {
protected String getLivingSound() {
return "entity.axolotl.idle_air";
}

@Override
public InteractionResult handlePlayerInteraction(final Player entityhuman, InteractionHand enumhand, final ItemStack itemStack) {
if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
return InteractionResult.CONSUME;
}

if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
if (Configuration.MyPet.Hoglin.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
itemStack.shrink(1);
if (itemStack.getCount() <= 0) {
entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
}
}
getMyPet().setBaby(false);
return InteractionResult.CONSUME;
}
}
return InteractionResult.PASS;
}

@Override
protected void defineSynchedData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

@EntitySize(width = 0.6F, height = 0.6f)
Expand Down Expand Up @@ -118,6 +122,27 @@ public void onLivingUpdate() {
}
}
}

@Override
public InteractionResult handlePlayerInteraction(final Player entityhuman, InteractionHand enumhand, final ItemStack itemStack) {
if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
return InteractionResult.CONSUME;
}

if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
if (Configuration.MyPet.Hoglin.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
itemStack.shrink(1);
if (itemStack.getCount() <= 0) {
entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
}
}
getMyPet().setBaby(false);
return InteractionResult.CONSUME;
}
}
return InteractionResult.PASS;
}

@Override
protected void doMyPetTick() {
Expand Down

0 comments on commit 474496f

Please sign in to comment.