com.github.shynixn.petblocks.sponge.nms.NMSRegistry Maven / Gradle / Ivy
package com.github.shynixn.petblocks.sponge.nms;
import com.github.shynixn.petblocks.api.business.entity.PetBlock;
import com.github.shynixn.petblocks.api.persistence.entity.PetMeta;
import com.github.shynixn.petblocks.sponge.nms.helper.PetBlockWrapper;
import org.spongepowered.api.entity.Transform;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.world.World;
/**
* Created by Shynixn 2017.
*
* Version 1.1
*
* MIT License
*
* Copyright (c) 2017 by Shynixn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
public class NMSRegistry {
private static CustomEntityType.WrappedRegistry wrappedRegistry;
private static final Class> rabbitClazz;
private static final Class> zombieClazz;
static {
try {
wrappedRegistry = (CustomEntityType.WrappedRegistry) findClassFromVersion("com.github.shynixn.petblocks.sponge.nms.VERSION.CustomEntityRegistry").newInstance();
rabbitClazz = findClassFromVersion("com.github.shynixn.petblocks.sponge.nms.VERSION.CustomRabbit");
zombieClazz = findClassFromVersion("com.github.shynixn.petblocks.sponge.nms.VERSION.CustomZombie");
} catch (final ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
}
}
private NMSRegistry() {
super();
}
/**
* Creates a new petblock from the given location and meta.
*
* @param location location
* @param meta meta
* @return petblock
*/
public static PetBlock> createPetBlock(Transform location, PetMeta meta) {
try {
if (!wrappedRegistry.isRegistered(rabbitClazz)) {
wrappedRegistry.register(rabbitClazz, CustomEntityType.RABBIT);
wrappedRegistry.register(zombieClazz, CustomEntityType.ZOMBIE);
}
return new PetBlockWrapper(location, meta.getPlayerMeta().getPlayer(), meta);
} catch (final Exception e) {
throw new RuntimeException("Cannot create petblock.", e);
}
}
/**
* Unregisters all custom entities.
*
* @throws Exception exception
*/
public static void unregisterCustomEntities() throws Exception {
if (wrappedRegistry != null) {
wrappedRegistry.unregister(rabbitClazz, CustomEntityType.RABBIT);
wrappedRegistry.unregister(zombieClazz, CustomEntityType.ZOMBIE);
wrappedRegistry = null;
}
}
/**
* Returns the class managed by version
*
* @param path path
* @return class
* @throws ClassNotFoundException exception
*/
private static Class> findClassFromVersion(String path) throws ClassNotFoundException {
return Class.forName(path.replace("VERSION", VersionSupport.getServerVersion().getVersionText()));
}
}