Moved book lores to NBT, so much better

This commit is contained in:
Vaern 2023-06-18 14:30:51 -07:00
parent 0a8ec4a389
commit 6e04fcd720
4 changed files with 83 additions and 18 deletions

View File

@ -27,7 +27,7 @@ public class GUIBookLore extends GuiScreen {
protected static int sizeX = 272;
protected static int sizeY = 182;
protected BookLoreType type;
protected String key;
protected NBTTagCompound tag;
//judgement
@ -39,19 +39,19 @@ public class GUIBookLore extends GuiScreen {
public GUIBookLore(EntityPlayer player) {
ItemStack stack = player.getHeldItem();
if(!stack.hasTagCompound()) return;
this.type = BookLoreType.getTypeFromStack(stack);
if(type == null) return;
this.tag = stack.getTagCompound();
this.key = tag.getString("k");
if(key.isEmpty()) return;
this.color = tag.getInteger("cov_col");
if(color <= 0)
color = 0x303030;
this.maxPage = (int)Math.ceil(type.pages / 2D) - 1;
this.maxPage = (int)Math.ceil(tag.getInteger("p") / 2D) - 1;
}
@Override
public void initGui() {
if(type == null) this.mc.thePlayer.closeScreen();
if(key == null || key.isEmpty()) this.mc.thePlayer.closeScreen();
this.guiLeft = (this.width - this.sizeX) / 2;
this.guiTop = (this.height - this.sizeY) / 2;
}
@ -93,13 +93,30 @@ public class GUIBookLore extends GuiScreen {
}
protected void drawGuiContainerForegroundLayer(int x, int y) {
String key = "book_lore." + type.keyI18n + ".page.";
String k = "book_lore." + key + ".page.";
for(int i = 0; i < 2; i++) {
int defacto = this.page * 2 + i; //TODO: force i18n to index from 0 instead of 1
if(defacto < this.type.pages) {
String text = I18nUtil.resolveKey(key + defacto); //TODO tag-based argument formatting
if(defacto < tag.getInteger("p")) {
String text;
NBTTagCompound argTag = tag.getCompoundTag("p" + defacto);
if(argTag.hasNoTags())
text = I18nUtil.resolveKey(k + defacto);
else {
List<String> args = new ArrayList();
int index = 1;
String arg = argTag.getString("a1");
while(!arg.isEmpty()) {
args.add(arg);
index++;
arg = argTag.getString("a" + index);
}
text = I18nUtil.resolveKey(k + defacto, args.toArray());
}
float scale = 1;
int width = 100;

View File

@ -2,7 +2,10 @@ package com.hbm.items.special;
import java.util.List;
import org.apache.commons.lang3.math.NumberUtils;
import com.hbm.inventory.gui.GUIBookLore;
import com.hbm.items.ModItems;
import com.hbm.lib.RefStrings;
import com.hbm.main.MainRegistry;
import com.hbm.tileentity.IGUIProvider;
@ -41,20 +44,22 @@ public class ItemBookLore extends Item implements IGUIProvider {
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool) {
BookLoreType type = BookLoreType.getTypeFromStack(stack);
if(!stack.hasTagCompound()) return;
String key = stack.stackTagCompound.getString("k");
if(key.isEmpty()) return;
if(type.hasAuthor) {
String unloc = I18nUtil.resolveKey("book_lore.author", I18nUtil.resolveKey("book_lore." + type.keyI18n + ".author"));
list.add(unloc);
}
key = "book_lore." + key + ".author";
String loc = I18nUtil.resolveKey(key);
list.add(I18nUtil.resolveKey("book_lore.author", loc));
}
@Override
public String getUnlocalizedName(ItemStack stack) {
BookLoreType type = BookLoreType.getTypeFromStack(stack);
if(!stack.hasTagCompound()) return "book_lore.test";
String key = stack.stackTagCompound.getString("k");
return "book_lore." + type.keyI18n;
return "book_lore." + (key.isEmpty() ? "test" : key);
}
//Textures
@ -116,6 +121,28 @@ public class ItemBookLore extends Item implements IGUIProvider {
return new GUIBookLore(player);
}
public static ItemStack createBook(String key, int pages, int colorCov, int colorTit) {
ItemStack book = new ItemStack(ModItems.book_lore);
NBTTagCompound tag = new NBTTagCompound();
tag.setString("k", key);
tag.setShort("p", (short)pages);
tag.setInteger("cov_col", colorCov);
tag.setInteger("tit_col", colorTit);
book.stackTagCompound = tag;
return book;
}
public static void addArgs(ItemStack book, int page, String... args) {
if(!book.hasTagCompound()) return;
NBTTagCompound data = new NBTTagCompound();
for(int i = 0; i < args.length; i++) {
data.setString("a" + (i + 1), args[i]);
}
book.stackTagCompound.setTag("p" + page, data);
}
//TODO remove this and fix any references
public enum BookLoreType {
TEST(true, "test", 5),
BOOK_IODINE(true, "book_iodine", 3) {

View File

@ -2,8 +2,11 @@ package com.hbm.items.tool;
import java.util.List;
import com.hbm.crafting.handlers.MKUCraftingHandler;
import com.hbm.handler.pollution.PollutionHandler;
import com.hbm.handler.pollution.PollutionHandler.PollutionType;
import com.hbm.items.ModItems;
import com.hbm.items.special.ItemBookLore;
import com.hbm.lib.Library;
import net.minecraft.entity.player.EntityPlayer;
@ -32,7 +35,25 @@ public class ItemWandD extends Item {
vnt.setSFX(new ExplosionEffectStandard());
vnt.explode();*/
PollutionHandler.incrementPollution(world, pos.blockX, pos.blockY, pos.blockZ, PollutionType.SOOT, 15);
MKUCraftingHandler.generateRecipe(world);
ItemStack[] recipe = MKUCraftingHandler.MKURecipe;
if(recipe == null) //take no chances
return stack;
int r = 0;
for(int i = 0; i < 9; i++) {
if(recipe[i] != null && recipe[i].getItem() == ModItems.powder_iodine) {
r = i + 1;
}
}
ItemStack book = ItemBookLore.createBook("book_iodine", 3, 0x4C407A, 0xFFF7C1);
ItemBookLore.addArgs(book, 2, String.valueOf(r));
player.inventory.addItemStackToInventory(book);
player.inventoryContainer.detectAndSendChanges();
//PollutionHandler.incrementPollution(world, pos.blockX, pos.blockY, pos.blockZ, PollutionType.SOOT, 15);
/*TimeAnalyzer.startCount("setBlock");
world.setBlock(pos.blockX, pos.blockY, pos.blockZ, Blocks.dirt);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

After

Width:  |  Height:  |  Size: 244 B