Refactor everything to WayofTime.bloodmagic.*
This commit is contained in:
parent
46742a73d1
commit
096ba02450
771 changed files with 566 additions and 573 deletions
|
@ -0,0 +1,76 @@
|
|||
package WayofTime.bloodmagic.api.util.helper;
|
||||
|
||||
import WayofTime.bloodmagic.api.NBTHolder;
|
||||
import WayofTime.bloodmagic.api.event.ItemBindEvent;
|
||||
import WayofTime.bloodmagic.api.iface.IBindable;
|
||||
import com.google.common.base.Strings;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
public class BindableHelper {
|
||||
|
||||
/**
|
||||
* Bind an item to a player. Handles checking if the player was an instanceof
|
||||
* {@link net.minecraftforge.common.util.FakePlayer} or other type of Fake Player.
|
||||
*
|
||||
* @param stack - The ItemStack to bind
|
||||
* @param player - The Player to bind the ItemStack to
|
||||
* @return - Whether binding was successful
|
||||
*/
|
||||
public static boolean checkAndSetItemOwner(ItemStack stack, EntityPlayer player) {
|
||||
return !PlayerHelper.isFakePlayer(player) && checkAndSetItemOwner(stack, player.getGameProfile().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind an item to a username.
|
||||
*
|
||||
* Requires the Item contained in the ItemStack to be an instanceof {@link IBindable}
|
||||
*
|
||||
* Fires {@link ItemBindEvent}.
|
||||
*
|
||||
* @param stack - The ItemStack to bind
|
||||
* @param ownerName - The username to bind the ItemStack to
|
||||
* @return - Whether the binding was successful
|
||||
*/
|
||||
public static boolean checkAndSetItemOwner(ItemStack stack, String ownerName) {
|
||||
stack = NBTHolder.checkNBT(stack);
|
||||
|
||||
if (!(stack.getItem() instanceof IBindable))
|
||||
return false;
|
||||
|
||||
if (Strings.isNullOrEmpty(stack.getTagCompound().getString(NBTHolder.NBT_OWNER))) {
|
||||
MinecraftForge.EVENT_BUS.post(new ItemBindEvent(PlayerHelper.getPlayerFromUsername(ownerName), ownerName, stack));
|
||||
((IBindable) stack.getItem()).onBind(PlayerHelper.getPlayerFromUsername(ownerName), stack);
|
||||
stack.getTagCompound().setString(NBTHolder.NBT_OWNER, ownerName);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Owner of the item without checking if it is already bound.
|
||||
* Also bypasses {@link ItemBindEvent}.
|
||||
*
|
||||
* @param stack - The ItemStack to bind
|
||||
* @param ownerName - The username to bind the ItemStack to
|
||||
*/
|
||||
public static void setItemOwner(ItemStack stack, String ownerName) {
|
||||
stack = NBTHolder.checkNBT(stack);
|
||||
|
||||
stack.getTagCompound().setString(NBTHolder.NBT_OWNER, ownerName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to safely obtain the username of the ItemStack's owner
|
||||
*
|
||||
* @param stack - The ItemStack to check the owner of
|
||||
* @return - The username of the ItemStack's owner
|
||||
*/
|
||||
public static String getOwnerName(ItemStack stack) {
|
||||
stack = NBTHolder.checkNBT(stack);
|
||||
|
||||
return stack.getTagCompound().getString(NBTHolder.NBT_OWNER);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package WayofTime.bloodmagic.api.util.helper;
|
||||
|
||||
import WayofTime.bloodmagic.api.BloodMagicAPI;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class LogHelper {
|
||||
|
||||
private Logger logger;
|
||||
|
||||
public LogHelper(String logger) {
|
||||
this.logger = LogManager.getLogger(logger);
|
||||
}
|
||||
|
||||
public void info(Object info) {
|
||||
if (BloodMagicAPI.isLoggingEnabled())
|
||||
logger.info(info);
|
||||
}
|
||||
|
||||
public void error(Object error) {
|
||||
if (BloodMagicAPI.isLoggingEnabled())
|
||||
logger.info(error);
|
||||
}
|
||||
|
||||
public void debug(Object debug) {
|
||||
if (BloodMagicAPI.isLoggingEnabled())
|
||||
logger.info(debug);
|
||||
}
|
||||
|
||||
public void fatal(Object fatal) {
|
||||
logger.fatal(fatal);
|
||||
}
|
||||
|
||||
public Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,232 @@
|
|||
package WayofTime.bloodmagic.api.util.helper;
|
||||
|
||||
import WayofTime.bloodmagic.api.BloodMagicAPI;
|
||||
import WayofTime.bloodmagic.api.NBTHolder;
|
||||
import WayofTime.bloodmagic.api.event.AddToNetworkEvent;
|
||||
import WayofTime.bloodmagic.api.event.SoulNetworkEvent;
|
||||
import WayofTime.bloodmagic.api.network.SoulNetwork;
|
||||
import com.google.common.base.Strings;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
|
||||
public class NetworkHelper {
|
||||
|
||||
// Get
|
||||
|
||||
public static SoulNetwork getSoulNetwork(String ownerName, World world) {
|
||||
SoulNetwork network = (SoulNetwork) world.getMapStorage().loadData(SoulNetwork.class, ownerName);
|
||||
|
||||
if (network == null) {
|
||||
network = new SoulNetwork(ownerName);
|
||||
world.getMapStorage().setData(ownerName, network);
|
||||
}
|
||||
|
||||
return network;
|
||||
}
|
||||
|
||||
// Syphon
|
||||
|
||||
/**
|
||||
* Master method used to syphon from the player's network, and will damage them accordingly if they do not have enough LP.
|
||||
* Does not drain on the client side.
|
||||
*
|
||||
* @param stack Owned itemStack
|
||||
* @param player Player using the item
|
||||
* @param syphon
|
||||
* @return True if the action should be executed and false if it should not. Always returns false if client-sided.
|
||||
*/
|
||||
public static boolean syphonAndDamageFromNetwork(ItemStack stack, EntityPlayer player, int syphon) {
|
||||
if (player.worldObj.isRemote)
|
||||
return false;
|
||||
|
||||
stack = NBTHolder.checkNBT(stack);
|
||||
String ownerName = stack.getTagCompound().getString(NBTHolder.NBT_OWNER);
|
||||
|
||||
if (!Strings.isNullOrEmpty(ownerName)) {
|
||||
SoulNetworkEvent.ItemDrainNetworkEvent event = new SoulNetworkEvent.ItemDrainNetworkEvent(player, ownerName, stack, syphon);
|
||||
|
||||
if(MinecraftForge.EVENT_BUS.post(event))
|
||||
return false;
|
||||
|
||||
int drainAmount = syphonFromNetwork(event.ownerName, event.syphon);
|
||||
|
||||
if(drainAmount == 0 || event.shouldDamage)
|
||||
hurtPlayer(player, event.syphon);
|
||||
|
||||
//The event has been told to prevent the action but allow all repercussions of using the item.
|
||||
return event.getResult() != Event.Result.DENY;
|
||||
}
|
||||
|
||||
int amount = NetworkHelper.syphonFromNetwork(stack, syphon);
|
||||
|
||||
hurtPlayer(player, syphon - amount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean syphonFromNetworkWhileInContainer(ItemStack stack, int syphon) {
|
||||
stack = NBTHolder.checkNBT(stack);
|
||||
String ownerName = stack.getTagCompound().getString(NBTHolder.NBT_OWNER);
|
||||
|
||||
if (Strings.isNullOrEmpty(ownerName))
|
||||
return false;
|
||||
|
||||
SoulNetworkEvent.ItemDrainInContainerEvent event = new SoulNetworkEvent.ItemDrainInContainerEvent(stack, ownerName, syphon);
|
||||
|
||||
if(MinecraftForge.EVENT_BUS.post(event) || event.getResult() == Event.Result.DENY)
|
||||
return false;
|
||||
|
||||
return syphonFromNetwork(event.ownerName, event.syphon) >= syphon;
|
||||
}
|
||||
|
||||
public static int syphonFromNetwork(ItemStack stack, int syphon) {
|
||||
stack = NBTHolder.checkNBT(stack);
|
||||
String ownerName = stack.getTagCompound().getString(NBTHolder.NBT_OWNER);
|
||||
if (!Strings.isNullOrEmpty(ownerName))
|
||||
return syphonFromNetwork(ownerName, syphon);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int syphonFromNetwork(String ownerName, int syphon) {
|
||||
if (MinecraftServer.getServer() == null)
|
||||
return 0;
|
||||
|
||||
World world = MinecraftServer.getServer().worldServers[0];
|
||||
SoulNetwork network = (SoulNetwork) world.loadItemData(SoulNetwork.class, ownerName);
|
||||
|
||||
if (network == null) {
|
||||
network = new SoulNetwork(ownerName);
|
||||
world.setItemData(ownerName, network);
|
||||
}
|
||||
|
||||
if (network.getCurrentEssence() >= syphon) {
|
||||
network.setCurrentEssence(network.getCurrentEssence() - syphon);
|
||||
network.markDirty();
|
||||
return syphon;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Add
|
||||
|
||||
/**
|
||||
* A method to add to an owner's network up to a maximum value.
|
||||
*
|
||||
* @return amount added to the network
|
||||
*/
|
||||
public static int addCurrentEssenceToMaximum(String ownerName, int addedEssence, int maximum) {
|
||||
AddToNetworkEvent event = new AddToNetworkEvent(ownerName, addedEssence, maximum);
|
||||
|
||||
if(MinecraftForge.EVENT_BUS.post(event))
|
||||
return 0;
|
||||
|
||||
if (MinecraftServer.getServer() == null)
|
||||
return 0;
|
||||
|
||||
World world = MinecraftServer.getServer().worldServers[0];
|
||||
SoulNetwork data = (SoulNetwork) world.loadItemData(SoulNetwork.class, event.ownerNetwork);
|
||||
|
||||
if (data == null) {
|
||||
data = new SoulNetwork(event.ownerNetwork);
|
||||
world.setItemData(event.ownerNetwork, data);
|
||||
}
|
||||
|
||||
int currEss = data.getCurrentEssence();
|
||||
|
||||
if (currEss >= event.maximum)
|
||||
return 0;
|
||||
|
||||
int newEss = Math.min(event.maximum, currEss + event.addedAmount);
|
||||
if(event.getResult() != Event.Result.DENY)
|
||||
data.setCurrentEssence(newEss);
|
||||
|
||||
return newEss - currEss;
|
||||
}
|
||||
|
||||
// Get
|
||||
|
||||
public static int getCurrentEssence(String ownerName) {
|
||||
if (MinecraftServer.getServer() == null)
|
||||
return 0;
|
||||
|
||||
World world = MinecraftServer.getServer().worldServers[0];
|
||||
SoulNetwork network = (SoulNetwork) world.loadItemData(SoulNetwork.class, ownerName);
|
||||
|
||||
if (network == null) {
|
||||
network = new SoulNetwork(ownerName);
|
||||
world.setItemData(ownerName, network);
|
||||
}
|
||||
|
||||
return network.getCurrentEssence();
|
||||
}
|
||||
|
||||
// Do damage
|
||||
|
||||
public static void hurtPlayer(EntityPlayer user, int energySyphoned) {
|
||||
if (energySyphoned < 100 && energySyphoned > 0) {
|
||||
if (!user.capabilities.isCreativeMode) {
|
||||
user.setHealth((user.getHealth() - 1));
|
||||
|
||||
if (user.getHealth() <= 0.0005f)
|
||||
user.onDeath(BloodMagicAPI.getDamageSource());
|
||||
}
|
||||
} else if (energySyphoned >= 100) {
|
||||
if (!user.capabilities.isCreativeMode) {
|
||||
for (int i = 0; i < ((energySyphoned + 99) / 100); i++) {
|
||||
user.setHealth((user.getHealth() - 1));
|
||||
|
||||
if (user.getHealth() <= 0.0005f) {
|
||||
user.onDeath(BloodMagicAPI.getDamageSource());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void hurtPlayer(EntityPlayer user, float damage) {
|
||||
if (!user.capabilities.isCreativeMode) {
|
||||
user.setHealth((user.getHealth() - damage));
|
||||
|
||||
if (user.getHealth() <= 0.0005f)
|
||||
user.onDeath(BloodMagicAPI.getDamageSource());
|
||||
}
|
||||
}
|
||||
|
||||
public static void setMaxOrbToMax(String ownerName, int maxOrb) {
|
||||
if (MinecraftServer.getServer() == null)
|
||||
return;
|
||||
|
||||
World world = MinecraftServer.getServer().worldServers[0];
|
||||
SoulNetwork network = (SoulNetwork) world.loadItemData(SoulNetwork.class, ownerName);
|
||||
|
||||
if (network == null) {
|
||||
network = new SoulNetwork(ownerName);
|
||||
world.setItemData(ownerName, network);
|
||||
}
|
||||
|
||||
network.setMaxOrb(Math.max(maxOrb, network.getMaxOrb()));
|
||||
network.markDirty();
|
||||
}
|
||||
|
||||
public static int getCurrentMaxOrb(String ownerName) {
|
||||
if (MinecraftServer.getServer() == null)
|
||||
return 0;
|
||||
|
||||
World world = MinecraftServer.getServer().worldServers[0];
|
||||
SoulNetwork network = (SoulNetwork) world.loadItemData(SoulNetwork.class, ownerName);
|
||||
|
||||
if (network == null) {
|
||||
network = new SoulNetwork(ownerName);
|
||||
world.setItemData(ownerName, network);
|
||||
}
|
||||
|
||||
return network.getMaxOrb();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package WayofTime.bloodmagic.api.util.helper;
|
||||
|
||||
import WayofTime.bloodmagic.api.NBTHolder;
|
||||
import com.google.common.base.Strings;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.potion.Potion;
|
||||
import net.minecraft.potion.PotionEffect;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PlayerHelper {
|
||||
|
||||
private static final Pattern FAKE_PLAYER_PATTERN = Pattern.compile("^(?:\\[.*\\])|(?:ComputerCraft)$");
|
||||
|
||||
public static String getUsernameFromPlayer(EntityPlayer player) {
|
||||
return player.getGameProfile().getName();
|
||||
}
|
||||
|
||||
public static EntityPlayer getPlayerFromUsername(String username) {
|
||||
if (MinecraftServer.getServer() == null)
|
||||
return null;
|
||||
|
||||
return MinecraftServer.getServer().getConfigurationManager().getPlayerByUsername(username);
|
||||
}
|
||||
|
||||
public static UUID getUUIDFromPlayer(EntityPlayer player) {
|
||||
return player.getGameProfile().getId();
|
||||
}
|
||||
|
||||
public static boolean isFakePlayer(EntityPlayer player) {
|
||||
return player instanceof FakePlayer || FAKE_PLAYER_PATTERN.matcher(getUsernameFromPlayer(player)).matches();
|
||||
}
|
||||
|
||||
public static void causeNauseaToPlayer(ItemStack stack) {
|
||||
stack = NBTHolder.checkNBT(stack);
|
||||
|
||||
if (!Strings.isNullOrEmpty(stack.getTagCompound().getString(NBTHolder.NBT_OWNER)))
|
||||
causeNauseaToPlayer(stack.getTagCompound().getString(NBTHolder.NBT_OWNER));
|
||||
}
|
||||
|
||||
public static void causeNauseaToPlayer(String ownerName) {
|
||||
EntityPlayer player = getPlayerFromUsername(ownerName);
|
||||
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
player.addPotionEffect(new PotionEffect(Potion.confusion.id, 80));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
package WayofTime.bloodmagic.api.util.helper;
|
||||
|
||||
import WayofTime.bloodmagic.api.event.RitualEvent;
|
||||
import WayofTime.bloodmagic.api.registry.RitualRegistry;
|
||||
import WayofTime.bloodmagic.api.ritual.IMasterRitualStone;
|
||||
import WayofTime.bloodmagic.api.ritual.Ritual;
|
||||
import WayofTime.bloodmagic.api.ritual.imperfect.IImperfectRitualStone;
|
||||
import WayofTime.bloodmagic.api.ritual.imperfect.ImperfectRitual;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
import sun.misc.Launcher;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
|
||||
public class RitualHelper {
|
||||
|
||||
public static boolean canCrystalActivate(Ritual ritual, int crystalLevel) {
|
||||
return ritual.getCrystalLevel() <= crystalLevel && RitualRegistry.ritualEnabled(ritual);
|
||||
}
|
||||
|
||||
public static String getNextRitualKey(String currentKey) {
|
||||
int currentIndex = RitualRegistry.getIds().indexOf(currentKey);
|
||||
int nextIndex = RitualRegistry.getRituals().listIterator(currentIndex).nextIndex();
|
||||
|
||||
return RitualRegistry.getIds().get(nextIndex);
|
||||
}
|
||||
|
||||
public static String getPrevRitualKey(String currentKey) {
|
||||
int currentIndex = RitualRegistry.getIds().indexOf(currentKey);
|
||||
int previousIndex = RitualRegistry.getIds().listIterator(currentIndex).previousIndex();
|
||||
|
||||
return RitualRegistry.getIds().get(previousIndex);
|
||||
}
|
||||
|
||||
public static boolean activate(IMasterRitualStone masterRitualStone, Ritual ritual, EntityPlayer player) {
|
||||
String owner = masterRitualStone.getOwner();
|
||||
|
||||
RitualEvent.RitualActivatedEvent event = new RitualEvent.RitualActivatedEvent(masterRitualStone, owner, ritual, player, player.getHeldItem(), player.getHeldItem().getItemDamage());
|
||||
|
||||
if (MinecraftForge.EVENT_BUS.post(event) || event.getResult() == Event.Result.DENY)
|
||||
return false;
|
||||
|
||||
return RitualRegistry.ritualEnabled(ritual) && ritual.startRitual(masterRitualStone, player);
|
||||
}
|
||||
|
||||
public static void perform(IMasterRitualStone masterRitualStone, Ritual ritual) {
|
||||
String owner = masterRitualStone.getOwner();
|
||||
|
||||
RitualEvent.RitualRunEvent event = new RitualEvent.RitualRunEvent(masterRitualStone, owner, ritual);
|
||||
|
||||
if (MinecraftForge.EVENT_BUS.post(event) || event.getResult() == Event.Result.DENY)
|
||||
return;
|
||||
|
||||
if (RitualRegistry.ritualEnabled(ritual))
|
||||
ritual.performEffect(masterRitualStone);
|
||||
}
|
||||
|
||||
public static void stop(IMasterRitualStone masterRitualStone, Ritual ritual, Ritual.BreakType breakType) {
|
||||
String owner = masterRitualStone.getOwner();
|
||||
|
||||
RitualEvent.RitualStopEvent event = new RitualEvent.RitualStopEvent(masterRitualStone, owner, ritual, breakType);
|
||||
MinecraftForge.EVENT_BUS.post(event);
|
||||
|
||||
ritual.onRitualBroken(masterRitualStone, breakType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds your Ritual to the {@link RitualRegistry#enabledRituals} Map.
|
||||
* This is used to determine whether your effect is enabled or not.
|
||||
*
|
||||
* The config option will be created as {@code B:ClassName=true} with a comment of
|
||||
* {@code Enables the ClassName ritual}.
|
||||
*
|
||||
* Should be safe to modify at any point.
|
||||
*
|
||||
* @param config - Your mod's Forge {@link Configuration} object.
|
||||
* @param packageName - The package your Rituals are located in.
|
||||
* @param category - The config category to write to.
|
||||
*/
|
||||
public static void checkRituals(Configuration config, String packageName, String category) {
|
||||
String name = packageName;
|
||||
if (!name.startsWith("/"))
|
||||
name = "/" + name;
|
||||
|
||||
name = name.replace('.', '/');
|
||||
URL url = Launcher.class.getResource(name);
|
||||
File directory = new File(url.getFile());
|
||||
|
||||
if (directory.exists()) {
|
||||
String[] files = directory.list();
|
||||
|
||||
for (String file : files) {
|
||||
if (file.endsWith(".class")) {
|
||||
String className = file.substring(0, file.length() - 6);
|
||||
|
||||
try {
|
||||
Object o = Class.forName(packageName + "." + className).newInstance();
|
||||
|
||||
if (o instanceof Ritual)
|
||||
RitualRegistry.enabledRituals.put((Ritual) o, config.get(category, className, true).getBoolean());
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Imperfect {
|
||||
|
||||
public static boolean activate(IImperfectRitualStone imperfectRitualStone, ImperfectRitual imperfectRitual, EntityPlayer player) {
|
||||
String owner = imperfectRitualStone.getOwner();
|
||||
|
||||
RitualEvent.ImperfectRitualActivatedEvent event = new RitualEvent.ImperfectRitualActivatedEvent(imperfectRitualStone, owner, imperfectRitual);
|
||||
|
||||
if (MinecraftForge.EVENT_BUS.post(event) || event.getResult() == Event.Result.DENY)
|
||||
return false;
|
||||
|
||||
|
||||
return imperfectRitual.onActivate(imperfectRitualStone, player);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue