Skip to content

Commit

Permalink
Added automatic "enchantment.level.(number>10)" roman numerals comple…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
dries007 committed Jul 3, 2016
1 parent f2b3dc7 commit db7588b
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -30,7 +30,7 @@ apply plugin: "idea-utils"
apply plugin: "maven"

group = "net.doubledoordev.d3core"
version = "1.2.0"
version = "1.2.1"

targetCompatibility = 1.7
sourceCompatibility = 1.7
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/doubledoordev/d3core/D3Core.java
Expand Up @@ -31,6 +31,7 @@

package net.doubledoordev.d3core;

import net.doubledoordev.d3core.client.LanguageHelper;
import net.doubledoordev.d3core.util.*;
import net.minecraft.client.resources.I18n;
import net.minecraftforge.common.MinecraftForge;
Expand Down Expand Up @@ -121,6 +122,7 @@ public void postInit(FMLPostInitializationEvent event)
{
EndermanGriefing.init();
pastPost = true;
if (event.getSide().isClient()) LanguageHelper.run();
}

@Mod.EventHandler
Expand Down
123 changes: 123 additions & 0 deletions src/main/java/net/doubledoordev/d3core/client/LanguageHelper.java
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2014-2016, Dries007 & DoubleDoorDevelopment
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of DoubleDoorDevelopment nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

package net.doubledoordev.d3core.client;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.translation.LanguageMap;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* This whole file is a hack, and contains code not fit for human eyes. It may make you dumber.
* It's one and only purpose is to dynamically add roman numerals to the language file.
* It requires 2 lines in the access transformers, since I didn't feel like using reflection.
* It also has to register a ReloadListener with the ResourceManager since language files can be reloaded.
* It also figures out the maximum possible enchant level by setting an Enchantment on an actual ItemStack, since mojang code does weird casting.
*
*/
public class LanguageHelper
{
private final static LinkedHashMap<String, Integer> ROMAN_NUMERALS = new LinkedHashMap<>();
static {
ROMAN_NUMERALS.put("M", 1000);
ROMAN_NUMERALS.put("CM", 900);
ROMAN_NUMERALS.put("D", 500);
ROMAN_NUMERALS.put("CD", 400);
ROMAN_NUMERALS.put("C", 100);
ROMAN_NUMERALS.put("XC", 90);
ROMAN_NUMERALS.put("L", 50);
ROMAN_NUMERALS.put("XL", 40);
ROMAN_NUMERALS.put("X", 10);
ROMAN_NUMERALS.put("IX", 9);
ROMAN_NUMERALS.put("V", 5);
ROMAN_NUMERALS.put("IV", 4);
ROMAN_NUMERALS.put("I", 1);
}
private static final String PREFIX = "enchantment.level.";

public static void run()
{
//noinspection NullableProblems
((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(new IResourceManagerReloadListener()
{
@Override
public void onResourceManagerReload(IResourceManager resourceManager)
{
Enchantment enchantment = null;
//noinspection StatementWithEmptyBody
for (int i = 0; enchantment == null; enchantment = Enchantment.REGISTRY.getObjectById(i++));
Item item = null;
//noinspection StatementWithEmptyBody
for (int i = 0; item == null; item = Item.REGISTRY.getObjectById(i++));
ItemStack s = new ItemStack(item);
s.addEnchantment(enchantment, Integer.MAX_VALUE);
final int max = EnchantmentHelper.getEnchantmentLevel(enchantment, s);
for (int i = 0; i < max; i++)
{
String key = PREFIX + i;
if (!I18n.i18nLocale.properties.containsKey(key))
{
String val = romanNumerals(i);
I18n.i18nLocale.properties.put(key, val);
}
}
LanguageMap.replaceWith(I18n.i18nLocale.properties);
}
});
}

/*
* Thanks stackoverflow:
* http://stackoverflow.com/questions/12967896/converting-integers-to-roman-numerals-java
*/
private static String romanNumerals(int Int)
{
StringBuilder builder = new StringBuilder();
for(Map.Entry<String, Integer> entry : ROMAN_NUMERALS.entrySet())
{
int matches = Int/entry.getValue();
String s = entry.getKey();
for (int i = 0; i < matches; i++) builder.append(s);
Int = Int % entry.getValue();
}
return builder.toString();
}
}
Expand Up @@ -32,15 +32,15 @@
package net.doubledoordev.d3core.client;

import net.doubledoordev.d3core.D3Core;
import net.doubledoordev.d3core.util.CoreConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.client.IModGuiFactory;
import net.minecraftforge.fml.client.config.GuiConfig;
import net.minecraftforge.fml.client.config.IConfigElement;
import net.doubledoordev.d3core.util.CoreConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;

import java.util.ArrayList;
import java.util.List;
Expand Down
Expand Up @@ -40,7 +40,6 @@
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;

import org.apache.commons.io.FileUtils;

import java.io.File;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/doubledoordev/d3core/util/Materials.java
Expand Up @@ -32,11 +32,11 @@
package net.doubledoordev.d3core.util;

import com.google.common.base.Strings;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.doubledoordev.d3core.D3Core;
import net.minecraft.item.Item;
import net.minecraft.item.Item.ToolMaterial;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.Loader;
import org.apache.commons.io.FileUtils;

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/D3Core_at.cfg
Expand Up @@ -2,3 +2,7 @@
public net.minecraft.entity.player.EntityPlayer field_71076_b
# Make lastDamage in EntityLivingBase public for VoidRefunds
public net.minecraft.entity.EntityLivingBase field_110153_bc
# Make I18n.i18nLocale public
public net.minecraft.client.resources.I18n field_135054_a
# Make Locale.properties public
public net.minecraft.client.resources.Locale field_135032_a

0 comments on commit db7588b

Please sign in to comment.