Skip to content

Commit

Permalink
Fix errors riding
Browse files Browse the repository at this point in the history
1.9 still has issues with riding though
Rename SpawnUtil to NMS
  • Loading branch information
Techcable committed Mar 23, 2016
1 parent 3757fa3 commit bc941f2
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import com.dsh105.echopet.compat.api.plugin.hook.IVanishProvider;
import com.dsh105.echopet.compat.api.plugin.hook.IWorldGuardProvider;
import com.dsh105.echopet.compat.api.registration.PetRegistry;
import com.dsh105.echopet.compat.api.util.ISpawnUtil;
import com.dsh105.echopet.compat.api.util.INMS;
import com.jolbox.bonecp.BoneCP;
import org.bukkit.plugin.Plugin;

public interface IEchoPetPlugin extends Plugin {

public ISpawnUtil getSpawnUtil();
public INMS getSpawnUtil();

public String getPrefix();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* This file is part of EchoPet.
*
* EchoPet is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EchoPet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EchoPet. If not, see <http://www.gnu.org/licenses/>.
*/

package com.dsh105.echopet.compat.api.util;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

import com.dsh105.echopet.compat.api.entity.IEntityPet;
import com.dsh105.echopet.compat.api.entity.IPet;
import com.google.common.base.Preconditions;

import net.techcable.sonarpet.utils.Versioning;
import net.techcable.sonarpet.utils.reflection.Reflection;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import static net.techcable.sonarpet.utils.Versioning.NMS_VERSION;

@SuppressWarnings("deprecation")
public interface INMS {

public IEntityPet spawn(IPet pet, Player owner);

public default void mount(Entity rider, Entity vehicle) {
Preconditions.checkNotNull(rider, "Null rider");
if (vehicle != null) {
rider.setPassenger(vehicle);
} else {
rider.leaveVehicle();
}
}

public static boolean isSupported() {
return Helper.instance != null;
}

public static INMS getInstance() {
if (isSupported()) {
return Helper.instance;
} else {
throw new UnsupportedOperationException("Unsupported version");
}
}

}

/**
* A helper for NMS getInstance()
*/
@Deprecated
class Helper {

public static final INMS instance;

static {
MethodHandle constructor;
Class<?> implClass = Reflection.getClass("com.dsh105.echopet.compat.nms." + NMS_VERSION + ".NMSImpl");
if (implClass == null) {
instance = null;
} else {
try {
constructor = MethodHandles.publicLookup().findConstructor(implClass, MethodType.methodType(void.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new AssertionError("Unable to invoke constructor", e);
}
try {
instance = constructor == null ? null : (INMS) constructor.invoke();
} catch (Throwable t) {
throw new AssertionError("NMS constructor threw exception", t);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.dsh105.echopet.compat.api.plugin.*;
import com.dsh105.echopet.compat.api.plugin.data.Updater;
import com.dsh105.echopet.compat.api.plugin.uuid.UUIDMigration;
import com.dsh105.echopet.compat.api.reflection.SafeConstructor;
import com.dsh105.echopet.compat.api.reflection.utility.CommonReflection;
import com.dsh105.echopet.compat.api.registration.PetRegistry;
import com.dsh105.echopet.compat.api.util.*;
Expand Down Expand Up @@ -61,7 +60,6 @@ public class EchoPetPlugin extends JavaPlugin implements IEchoPetPlugin {

private static boolean isUsingNetty;

private static ISpawnUtil SPAWN_UTIL;
private static PetManager MANAGER;
private static SqlPetManager SQL_MANAGER;
private static ConfigOptions OPTIONS;
Expand Down Expand Up @@ -103,9 +101,7 @@ public void onEnable() {
COMMAND_MANAGER = new CommandManager(this);
// Make sure that the plugin is running under the correct version to prevent errors

try {
Class.forName(ReflectionUtil.COMPAT_NMS_PATH + ".SpawnUtil");
} catch (ClassNotFoundException e) {
if (!INMS.isSupported()) {
EchoPet.LOG.log(ChatColor.RED + "SonarPet " + ChatColor.GOLD
+ this.getDescription().getVersion() + ChatColor.RED
+ " is not compatible with this version of CraftBukkit");
Expand All @@ -122,8 +118,6 @@ public void onEnable() {

this.petRegistry = new PetRegistry();

SPAWN_UTIL = new SafeConstructor<ISpawnUtil>(ReflectionUtil.getVersionedClass("SpawnUtil")).newInstance();

this.loadConfiguration();

PluginManager manager = getServer().getPluginManager();
Expand Down Expand Up @@ -389,8 +383,8 @@ public YAMLConfig getLangConfig() {
}

@Override
public ISpawnUtil getSpawnUtil() {
return SPAWN_UTIL;
public INMS getSpawnUtil() {
return INMS.getInstance();
}

@Override
Expand Down
27 changes: 11 additions & 16 deletions modules/EchoPet/src/main/java/com/dsh105/echopet/api/pet/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.dsh105.echopet.compat.api.reflection.ReflectionConstants;
import com.dsh105.echopet.compat.api.reflection.SafeMethod;
import com.dsh105.echopet.compat.api.util.Lang;
import com.dsh105.echopet.compat.api.util.INMS;
import com.dsh105.echopet.compat.api.util.PetNames;
import com.dsh105.echopet.compat.api.util.PlayerUtil;
import com.dsh105.echopet.compat.api.util.ReflectionUtil;
Expand Down Expand Up @@ -293,10 +294,8 @@ public void ownerRidePet(boolean flag) {

// Ew...This stuff is UGLY :c

final SafeMethod method = new SafeMethod(ReflectionUtil.getNMSClass("Entity"), ReflectionConstants.ENTITY_FUNC_MOUNT.getName(), ReflectionUtil.getNMSClass("Entity"));

if (!flag) {
method.invoke(PlayerUtil.playerToEntityPlayer(this.getOwner()), new Object[]{null});
INMS.getInstance().mount(this.getOwner(), null);
//((CraftPlayer) this.getOwner()).getHandle().mount(null);
if (this.getEntityPet() instanceof IEntityNoClipPet) {
((IEntityNoClipPet) this.getEntityPet()).noClip(true);
Expand All @@ -309,7 +308,7 @@ public void ownerRidePet(boolean flag) {
new BukkitRunnable() {
@Override
public void run() {
method.invoke(PlayerUtil.playerToEntityPlayer(getOwner()), getEntityPet());
INMS.getInstance().mount(getOwner(), getEntityPet().getBukkitEntity());
//((CraftPlayer) getOwner()).getHandle().mount(getEntityPet());
ownerIsMounting = false;
if (getEntityPet() instanceof IEntityNoClipPet) {
Expand Down Expand Up @@ -337,40 +336,36 @@ public void setAsHat(boolean flag) {
}
this.teleportToOwner();

// Ew...This stuff is UGLY :c

SafeMethod method = new SafeMethod(ReflectionUtil.getNMSClass("Entity"), ReflectionConstants.ENTITY_FUNC_MOUNT.getName(), ReflectionUtil.getNMSClass("Entity"));

//Entity craftPet = ((Entity) this.getCraftPet().getHandle());
if (!flag) {
if (this.getRider() != null) {
//Entity rider = ((Entity) this.getRider().getCraftPet().getHandle());
//rider.mount(null);
method.invoke(this.getRider().getEntityPet(), new Object[]{null});
INMS.getInstance().mount(this.getRider().getEntityPet().getBukkitEntity(), null);

//craftPet.mount(null);
method.invoke(this.getEntityPet(), new Object[]{null});
INMS.getInstance().mount(this.getEntityPet().getBukkitEntity(), null);

//rider.mount(craftPet);
method.invoke(this.getRider().getEntityPet(), this.getEntityPet());
INMS.getInstance().mount(this.getRider().getEntityPet().getBukkitEntity(), this.getEntityPet().getBukkitEntity());
} else {
//craftPet.mount(null);
method.invoke(this.getEntityPet(), new Object[]{null});
INMS.getInstance().mount(this.getEntityPet().getBukkitEntity(), null);
}
} else {
if (this.getRider() != null) {
//Entity rider = ((Entity) this.getRider().getCraftPet().getHandle());
//rider.mount(null);
method.invoke(this.getRider().getEntityPet(), new Object[]{null});
INMS.getInstance().mount(this.getRider().getEntityPet().getBukkitEntity(), null);

//craftPet.mount(((CraftPlayer) this.getOwner()).getHandle());
method.invoke(this.getEntityPet(), PlayerUtil.playerToEntityPlayer(this.getOwner()));
INMS.getInstance().mount(this.getEntityPet().getBukkitEntity(), this.getOwner());

//this.getCraftPet().setPassenger(this.getRider().getCraftPet());
method.invoke(this.getRider().getEntityPet(), this.getEntityPet());
INMS.getInstance().mount(this.getRider().getEntityPet().getBukkitEntity(), this.getEntityPet().getBukkitEntity());
} else {
//craftPet.mount(((CraftPlayer) this.getOwner()).getHandle());
method.invoke(this.getEntityPet(), PlayerUtil.playerToEntityPlayer(this.getOwner()));
INMS.getInstance().mount(this.getEntityPet().getBukkitEntity(), this.getOwner());
}
}
this.getEntityPet().resizeBoundingBox(flag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.dsh105.echopet.compat.api.entity.IPet;
import com.dsh105.echopet.compat.api.event.PetPreSpawnEvent;
import com.dsh105.echopet.compat.api.plugin.EchoPet;
import com.dsh105.echopet.compat.api.util.ISpawnUtil;
import com.dsh105.echopet.compat.api.util.INMS;
import com.dsh105.echopet.compat.nms.v1_6_R3.entity.EntityPet;

import net.minecraft.server.v1_6_R3.World;
Expand All @@ -32,7 +32,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;

public class SpawnUtil implements ISpawnUtil {
public class NMSImpl implements INMS {

@Override
public EntityPet spawn(IPet pet, Player owner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.dsh105.echopet.compat.api.entity.IPet;
import com.dsh105.echopet.compat.api.event.PetPreSpawnEvent;
import com.dsh105.echopet.compat.api.plugin.EchoPet;
import com.dsh105.echopet.compat.api.util.ISpawnUtil;
import com.dsh105.echopet.compat.api.util.INMS;
import com.dsh105.echopet.compat.nms.v1_7_R1.entity.EntityPet;

import net.minecraft.server.v1_7_R1.World;
Expand All @@ -32,7 +32,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;

public class SpawnUtil implements ISpawnUtil {
public class NMSImpl implements INMS {

@Override
public EntityPet spawn(IPet pet, Player owner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.dsh105.echopet.compat.api.entity.IPet;
import com.dsh105.echopet.compat.api.event.PetPreSpawnEvent;
import com.dsh105.echopet.compat.api.plugin.EchoPet;
import com.dsh105.echopet.compat.api.util.ISpawnUtil;
import com.dsh105.echopet.compat.api.util.INMS;
import com.dsh105.echopet.compat.nms.v1_7_R2.entity.EntityPet;

import net.minecraft.server.v1_7_R2.World;
Expand All @@ -32,7 +32,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;

public class SpawnUtil implements ISpawnUtil {
public class NMSImpl implements INMS {

@Override
public EntityPet spawn(IPet pet, Player owner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.dsh105.echopet.compat.api.entity.IPet;
import com.dsh105.echopet.compat.api.event.PetPreSpawnEvent;
import com.dsh105.echopet.compat.api.plugin.EchoPet;
import com.dsh105.echopet.compat.api.util.ISpawnUtil;
import com.dsh105.echopet.compat.api.util.INMS;
import com.dsh105.echopet.compat.nms.v1_7_R3.entity.EntityPet;

import net.minecraft.server.v1_7_R3.World;
Expand All @@ -32,7 +32,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;

public class SpawnUtil implements ISpawnUtil {
public class NMSImpl implements INMS {

@Override
public EntityPet spawn(IPet pet, Player owner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.dsh105.echopet.compat.api.entity.IPet;
import com.dsh105.echopet.compat.api.event.PetPreSpawnEvent;
import com.dsh105.echopet.compat.api.plugin.EchoPet;
import com.dsh105.echopet.compat.api.util.ISpawnUtil;
import com.dsh105.echopet.compat.api.util.INMS;
import com.dsh105.echopet.compat.nms.v1_7_R4.entity.EntityPet;

import net.minecraft.server.v1_7_R4.World;
Expand All @@ -32,7 +32,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;

public class SpawnUtil implements ISpawnUtil {
public class NMSImpl implements INMS {

@Override
public EntityPet spawn(IPet pet, Player owner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.dsh105.echopet.compat.api.entity.IPet;
import com.dsh105.echopet.compat.api.event.PetPreSpawnEvent;
import com.dsh105.echopet.compat.api.plugin.EchoPet;
import com.dsh105.echopet.compat.api.util.ISpawnUtil;
import com.dsh105.echopet.compat.api.util.INMS;
import com.dsh105.echopet.compat.nms.v1_8_R1.entity.EntityPet;

import net.minecraft.server.v1_8_R1.World;
Expand All @@ -32,7 +32,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;

public class SpawnUtil implements ISpawnUtil {
public class NMSImpl implements INMS {

@Override
public EntityPet spawn(IPet pet, Player owner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.dsh105.echopet.compat.api.entity.IPet;
import com.dsh105.echopet.compat.api.event.PetPreSpawnEvent;
import com.dsh105.echopet.compat.api.plugin.EchoPet;
import com.dsh105.echopet.compat.api.util.ISpawnUtil;
import com.dsh105.echopet.compat.api.util.INMS;
import com.dsh105.echopet.compat.nms.v1_8_R2.entity.EntityPet;

import net.minecraft.server.v1_8_R2.World;
Expand All @@ -32,7 +32,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;

public class SpawnUtil implements ISpawnUtil {
public class NMSImpl implements INMS {

@Override
public EntityPet spawn(IPet pet, Player owner) {
Expand Down
Loading

0 comments on commit bc941f2

Please sign in to comment.