Refactor everything to WayofTime.bloodmagic.*

This commit is contained in:
Nick 2015-11-02 12:39:44 -08:00
parent 46742a73d1
commit 096ba02450
771 changed files with 566 additions and 573 deletions

View file

@ -0,0 +1,238 @@
package WayofTime.bloodmagic.util;
import WayofTime.bloodmagic.network.AlchemicalWizardryPacketHandler;
import WayofTime.bloodmagic.util.helper.TextHelper;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
public class ChatUtil {
/**
* @author tterrag1098
*
* Ripped from EnderCore (and slightly altered)
*/
public static class PacketNoSpamChat implements IMessage {
private IChatComponent[] chatLines;
public PacketNoSpamChat() {
chatLines = new IChatComponent[0];
}
private PacketNoSpamChat(IChatComponent... lines) {
// this is guaranteed to be >1 length by accessing methods
this.chatLines = lines;
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(chatLines.length);
for (IChatComponent c : chatLines) {
ByteBufUtils.writeUTF8String(buf, IChatComponent.Serializer.componentToJson(c));
}
}
@Override
public void fromBytes(ByteBuf buf) {
chatLines = new IChatComponent[buf.readInt()];
for (int i = 0; i < chatLines.length; i++) {
chatLines[i] = IChatComponent.Serializer.jsonToComponent(ByteBufUtils.readUTF8String(buf));
}
}
public static class Handler implements IMessageHandler<PacketNoSpamChat, IMessage> {
@Override
public IMessage onMessage(PacketNoSpamChat message, MessageContext ctx) {
sendNoSpamMessages(message.chatLines);
return null;
}
}
}
private static final int DELETION_ID = 2525277;
private static int lastAdded;
private static void sendNoSpamMessages(IChatComponent[] messages) {
GuiNewChat chat = Minecraft.getMinecraft().ingameGUI.getChatGUI();
for (int i = DELETION_ID + messages.length - 1; i <= lastAdded; i++) {
chat.deleteChatLine(i);
}
for (int i = 0; i < messages.length; i++) {
chat.printChatMessageWithOptionalDeletion(messages[i], DELETION_ID + i);
}
lastAdded = DELETION_ID + messages.length - 1;
}
/**
* Returns a standard {@link ChatComponentText} for the given {@link String}.
*
* @param s
* The string to wrap.
* @return An {@link IChatComponent} containing the string.
*/
public static IChatComponent wrap(String s) {
return new ChatComponentText(s);
}
/**
* @see #wrap(String)
*/
public static IChatComponent[] wrap(String... s) {
IChatComponent[] ret = new IChatComponent[s.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = wrap(s[i]);
}
return ret;
}
/**
* Returns a translatable chat component for the given string and format args.
*
* @param s
* The string to format
* @param args
* The args to apply to the format
*/
public static IChatComponent wrapFormatted(String s, Object... args) {
return new ChatComponentTranslation(s, args);
}
/**
* Simply sends the passed lines to the player in a chat message.
*
* @param player
* The player to send the chat to
* @param lines
* The lines to send
*/
public static void sendChat(EntityPlayer player, String... lines) {
sendChat(player, wrap(lines));
}
/**
* Localizes the lines before sending them.
*
* @see #sendChat(EntityPlayer, String...)
*/
public static void sendChatUnloc(EntityPlayer player, String... unlocLines) {
sendChat(player, TextHelper.localizeAll(unlocLines));
}
/**
* Sends all passed chat components to the player.
*
* @param player
* The player to send the chat lines to.
* @param lines
* The {@link IChatComponent chat components} to send.yes
*/
public static void sendChat(EntityPlayer player, IChatComponent... lines) {
for (IChatComponent c : lines) {
player.addChatComponentMessage(c);
}
}
/**
* Localizes the strings before sending them.
*
* @see #sendNoSpamClient(String...)
*/
public static void sendNoSpamClientUnloc(String... unlocLines) {
sendNoSpamClient(TextHelper.localizeAll(unlocLines));
}
/**
* Same as {@link #sendNoSpamClient(IChatComponent...)}, but wraps the Strings
* automatically.
*
* @param lines
* The chat lines to send
* @see #wrap(String)
*/
public static void sendNoSpamClient(String... lines) {
sendNoSpamClient(wrap(lines));
}
/**
* Skips the packet sending, unsafe to call on servers.
*
* @see #sendNoSpam(EntityPlayerMP, IChatComponent...)
*/
public static void sendNoSpamClient(IChatComponent... lines) {
sendNoSpamMessages(lines);
}
/**
* Localizes the strings before sending them.
*
* @see #sendNoSpam(EntityPlayer, String...)
*/
public static void sendNoSpamUnloc(EntityPlayer player, String... unlocLines) {
sendNoSpam(player, TextHelper.localizeAll(unlocLines));
}
/**
* @see #wrap(String)
* @see #sendNoSpam(EntityPlayer, IChatComponent...)
*/
public static void sendNoSpam(EntityPlayer player, String... lines) {
sendNoSpam(player, wrap(lines));
}
/**
* First checks if the player is instanceof {@link EntityPlayerMP} before
* casting.
*
* @see #sendNoSpam(EntityPlayerMP, IChatComponent...)
*/
public static void sendNoSpam(EntityPlayer player, IChatComponent... lines) {
if (player instanceof EntityPlayerMP) {
sendNoSpam((EntityPlayerMP) player, lines);
}
}
/**
* Localizes the strings before sending them.
*
* @see #sendNoSpam(EntityPlayerMP, String...)
*/
public static void sendNoSpamUnloc(EntityPlayerMP player, String... unlocLines) {
sendNoSpam(player, TextHelper.localizeAll(unlocLines));
}
/**
* @see #wrap(String)
* @see #sendNoSpam(EntityPlayerMP, IChatComponent...)
*/
public static void sendNoSpam(EntityPlayerMP player, String... lines) {
sendNoSpam(player, wrap(lines));
}
/**
* Sends a chat message to the client, deleting past messages also sent via
* this method.
* <p>
* Credit to RWTema for the idea
*
* @param player
* The player to send the chat message to
* @param lines
* The chat lines to send.
*/
public static void sendNoSpam(EntityPlayerMP player, IChatComponent... lines) {
if (lines.length > 0)
AlchemicalWizardryPacketHandler.INSTANCE.sendTo(new PacketNoSpamChat(lines), player);
}
}

View file

@ -0,0 +1,16 @@
package WayofTime.bloodmagic.util;
public class Utils {
public static boolean isInteger(String integer) {
try {
Integer.parseInt(integer);
} catch(NumberFormatException e) {
return false;
} catch(NullPointerException e) {
return false;
}
// only got here if we didn't return false
return true;
}
}

View file

@ -0,0 +1,34 @@
package WayofTime.bloodmagic.util.handler;
import WayofTime.bloodmagic.registry.ModBlocks;
import WayofTime.bloodmagic.registry.ModItems;
import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.FillBucketEvent;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class EventHandler {
@SubscribeEvent
public void onBucketFill(FillBucketEvent event) {
if (event.current.getItem() != Items.bucket)
return;
ItemStack result = null;
Block block = event.world.getBlockState(event.target.getBlockPos()).getBlock();
if (block != null && (block.equals(ModBlocks.lifeEssence)) && block.getMetaFromState(event.world.getBlockState(event.target.getBlockPos())) == 0) {
event.world.setBlockToAir(event.target.getBlockPos());
result = new ItemStack(ModItems.bucketEssence);
}
if (result == null)
return;
event.result = result;
event.setResult(Event.Result.ALLOW);
}
}

View file

@ -0,0 +1,128 @@
package WayofTime.bloodmagic.util.helper;
import WayofTime.bloodmagic.BloodMagic;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.statemap.StateMapperBase;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.model.ModelLoader;
/**
* @author <a href="https://github.com/TehNut">TehNut</a>
*
* The goal of this class is to make registering the inventory renders
* for your Items/Blocks a much simpler and easier process.
*
* You must call this at the post initialization stage on
* the clientside only.
*
* If you pass a Block through here that uses the default
* ItemBlock, you should specify a custom name.
*/
public class InventoryRenderHelper {
/**
* This is the base string for your resources. It will usually be
* your modid in all lowercase with a colon at the end.
*/
private final String domain;
public InventoryRenderHelper(String domain) {
this.domain = domain;
}
/**
* Registers a Model for the given Item and meta.
*
* @param item - Item to register Model for
* @param meta - Meta of Item
* @param name - Name of the model JSON
*/
public void itemRender(Item item, int meta, String name) {
String resName = domain + name;
ModelBakery.addVariantName(item, resName);
ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(resName, "inventory"));
}
/**
* Shorthand of {@code itemRender(Item, int, String)}
*
* @param item - Item to register Model for
* @param meta - Meta of Item
*/
public void itemRender(Item item, int meta) {
itemRender(item, meta, getClassName(item) + meta);
}
public void itemRender(Item item, String name) {
itemRender(item, 0, name);
}
/**
* Shorthand of {@code itemRender(Item, int)}
*
* @param item - Item to register Model for
*/
public void itemRender(Item item) {
itemRender(item, 0, getClassName(item));
}
/**
* Registers a model for the item across all Meta's that get used for the item
*
* @param item - Item to register Model for
*/
public void itemRenderAll(Item item) {
final Item toRender = item;
ModelLoader.setCustomMeshDefinition(item, new ItemMeshDefinition() {
@Override
public ModelResourceLocation getModelLocation(ItemStack stack) {
return new ModelResourceLocation(domain + getClassName(toRender), "inventory");
}
});
}
public void itemRenderToggle(Item item, String name) {
itemRender(item, 0, name + "_deactivated");
itemRender(item, 1, name + "_activated");
}
public void fluidRender(Block block) {
final Block toRender = block;
ModelBakery.addVariantName(InventoryRenderHelper.getItemFromBlock(block));
ModelLoader.setCustomMeshDefinition(InventoryRenderHelper.getItemFromBlock(block), new ItemMeshDefinition() {
@Override
public ModelResourceLocation getModelLocation(ItemStack stack) {
return new ModelResourceLocation(BloodMagic.DOMAIN + toRender.getClass().getSimpleName(), "fluid");
}
});
ModelLoader.setCustomStateMapper(block, new StateMapperBase() {
@Override
protected ModelResourceLocation getModelResourceLocation(IBlockState state) {
return new ModelResourceLocation(domain + toRender.getClass().getSimpleName(), "fluid");
}
});
}
/**
*
* @param block - Block to get Item of
* @return - The ItemBlock that corresponds to the Block.
*/
public static Item getItemFromBlock(Block block) {
return Item.getItemFromBlock(block);
}
private static String getClassName(Item item) {
return item instanceof ItemBlock ? Block.getBlockFromItem(item).getClass().getSimpleName() : item.getClass().getSimpleName();
}
}

View file

@ -0,0 +1,40 @@
package WayofTime.bloodmagic.util.helper;
import com.google.common.collect.Lists;
import net.minecraft.util.StatCollector;
import java.util.List;
public class TextHelper {
public static String getFormattedText(String string) {
return string.replaceAll("&", "\u00A7");
}
public static String localize(String key, Object ... format) {
return getFormattedText(StatCollector.translateToLocalFormatted(key, format));
}
/**
* Localizes all strings in a list, using the prefix.
*
* @param unloc
* The list of unlocalized strings.
* @return A list of localized versions of the passed strings.
*/
public static List<String> localizeAll(List<String> unloc) {
List<String> ret = Lists.newArrayList();
for (String s : unloc)
ret.add(localize(s));
return ret;
}
public static String[] localizeAll(String... unloc) {
String[] ret = new String[unloc.length];
for (int i = 0; i < ret.length; i++)
ret[i] = localize(unloc[i]);
return ret;
}
}