Jump to content

Major Tuvok

Members
  • Posts

    62
  • Joined

  • Last visited

Everything posted by Major Tuvok

  1. (In this Code it would be better to place the suppress unchecked in RegistryUtils: private void checkAdditionalRegistration(T toRegister) at the place where it is really needed)
  2. Also Log Errors if you have some (or warnings for that matter)! (You see how many Logs I have in there...) It will make tracing much easier.
  3. public class SpellcraftBlocks extends BaseContainer<Block> { private static final int GENERATOR_WEIGHT = 2; public static BlockWandCraftingTable WAND_CRAFTING_TABLE; private RegistryUtils<Item> itemRegistryUtils; public SpellcraftBlocks() { super(); itemRegistryUtils = new RegistryUtils<>(); } @Override @SubscribeEvent public void onRegistryEvent(RegistryEvent.Register<Block> e) { createBlocks(); super.onRegistryEvent(e); Log.info("Registering SpellcraftBlocks"); register(WAND_CRAFTING_TABLE); Log.info("Adding SpellcraftItemBlocks"); registerBlockItem(WAND_CRAFTING_TABLE); } @SubscribeEvent public void onItemRegistryEvent(RegistryEvent.Register<Item> e) { Log.info("Registering SpellcraftItemBlocks"); itemRegistryUtils.setRegistry(e.getRegistry()); for (Item item : getUtils().getRegisteredItems()) { itemRegistryUtils.register(item); } } @Override public void commonPreInit() { super.commonPreInit(); } @Override @SideOnly(Side.CLIENT) public void clientInit() { super.clientInit(); } public void registerWorldGen() { Log.info("Registering Ore Generation"); } public void registerBlockItem(Block entry) { if (entry != null) { RenderableBlockItem blockItem = new RenderableBlockItem(entry); addToItemMap(entry, blockItem); addToRenderable(entry); } else { Log.error("Cannot registerBlockItem with null Block!"); } } private static void createBlocks() { Log.info("Creating Blocks"); WAND_CRAFTING_TABLE = new BlockWandCraftingTable(); } private static final class RenderableBlockItem extends ItemBlock implements INamed, IRenderable { public RenderableBlockItem(Block block) { super(block); if (block.getRegistryName() != null) { setRegistryName(block.getRegistryName()); } } @Override public String getName() { if (block instanceof INamed) { return ((INamed) block).getName(); } else { return block.getUnlocalizedName().substring(5); } } @Nullable @Override public ResourceLocation getLocation() { return getRegistryName(); } @Override public boolean registerRenderer() { return true; } } } In conjuction with: public class BaseContainer<T extends IForgeRegistryEntry<T>> implements ILoggable { private final RegistryUtils<T> utils; public BaseContainer() { this.utils = new RegistryUtils<T>(); MinecraftForge.EVENT_BUS.register(this); } protected RegistryUtils<T> getUtils() { return utils; } @SubscribeEvent public void onRegistryEvent(RegistryEvent.Register<T> e) { getUtils().setRegistry(e.getRegistry()); } public void commonPreInit() { } public void postInit() { getUtils().postInit(); } public @Nullable Item getItem(T thing) { return getUtils().getItem(thing); } @SideOnly(Side.CLIENT) public void clientInit() { getUtils().clientInit(); } protected void register(T thing) { getUtils().register(thing); } @SideOnly(Side.CLIENT) protected void registerRenderer(@Nonnull T thing) { getUtils().registerRenderer(thing); } @SideOnly(Side.CLIENT) protected void registerItemRenderer(@Nonnull Item thing) { getUtils().registerItemRenderer(thing); } @SideOnly(Side.CLIENT) protected void registerItemRenderer(@Nonnull Item item, @Nonnegative int meta) { getUtils().registerItemRenderer(item, meta); } protected Object registerOreDict(@Nonnull Object thing, String name) { return getUtils().registerOreDict(thing, name); } @SubscribeEvent public void registerRender(ModelRegistryEvent registryEvent) { getUtils().registerRenders(registryEvent); } protected IOreDictNamed registerOreDict(@Nonnull IOreDictNamed thing) { return getUtils().registerOreDict(thing); } protected void addToRenderable(T t) { getUtils().addToRenderable(t); } protected void addToItemMap(T thing, Item item) { getUtils().addToItemMap(thing, item); } } public class RegistryUtils<T extends IForgeRegistryEntry<T>> implements ILoggable { private List<T> renderable; private List<Item> itemRenderables; private List<Tuple<Object, String>> postponedOredictEntries; private HashMap<T, Item> thingItemMap; private IForgeRegistry<T> registry; private boolean initWasCalled; public RegistryUtils() { renderable = new LinkedList<>(); thingItemMap = new HashMap<>(); itemRenderables = new LinkedList<>(); registry = null; postponedOredictEntries = new LinkedList<>(); initWasCalled = false; } public void clientInit() { initWasCalled = true; for (Tuple<Object, String> tuple : postponedOredictEntries) { registerOreDict(tuple.getFirst(), tuple.getSecond()); } } public void postInit() { renderable.clear(); itemRenderables.clear(); postponedOredictEntries.clear(); thingItemMap.clear(); renderable = null; itemRenderables = null; postponedOredictEntries = null; thingItemMap = null; registry = null; initWasCalled = false; } public void setRegistry(IForgeRegistry<T> registry) { this.registry = registry; } public void registerRenders(ModelRegistryEvent registryEvent) { for (T thing : renderable) { if (thing instanceof Item) { itemRenderables.add((Item) thing); } else { Item item = getItem(thing); if (item != null) { itemRenderables.add(item); } else { Log.warn("Failed to registerGameOverlayListener Renderer for " + getName(item) + "!"); } } } for (Item item : itemRenderables) { registerItemRenderer(item); } } public void register(RegistryEvent.Register<T> e, T toRegister) { setRegistry(e.getRegistry()); checkAdditionalRegistration(toRegister); } public void registerAll(RegistryEvent.Register<T> e, T... toRegister) { setRegistry(e.getRegistry()); registerAll(toRegister); } public void register(T toRegister) { if (this.registry != null && toRegister != null) { this.registry.register(toRegister); checkAdditionalRegistration(toRegister); } else { Log.error("Failed to registerGameOverlayListener thing(" + (toRegister != null ? getName(toRegister) + ") because there was no registry available!" : "null) because thing was null")); } } public void registerAll(T... toRegister) { if (this.registry != null && toRegister != null && toRegister.length > 0) { this.registry.registerAll(toRegister); for (T thing : toRegister) { checkAdditionalRegistration(thing); } } else { Log.error("Failed to registerGameOverlayListener thing(" + (toRegister != null ? (toRegister.length > 0 ? Arrays.toString(toRegister) + ") because there was no registry available!" : "void vaargs list)") : "null vaargs list)")); } } @SuppressWarnings("unchecked") private void checkAdditionalRegistration(T toRegister) { if (toRegister instanceof IOreDictNamed) { registerOreDict((IOreDictNamed) toRegister); } if (toRegister instanceof TileEntityContainer) { GameRegistry.registerTileEntity(((TileEntityContainer) toRegister).getTileEntityClass(), MODID + getName(toRegister)); } addToRenderable(toRegister); } public @Nullable Item getItem(T thing) { if (thing instanceof Item) { return (Item) thing; } else if (thingItemMap.containsKey(thing)) { return thingItemMap.get(thing); } return null; } protected Object registerOreDict(Object thing, String name) { if (name != null && thing != null) { if (initWasCalled) { if (thing instanceof Block) { OreDictionary.registerOre(name, (Block) thing); } else if (thing instanceof Item) { OreDictionary.registerOre(name, (Item) thing); } else if (thing instanceof ItemStack) { OreDictionary.registerOre(name, (ItemStack) thing); } else { Log.error("Cannot registerGameOverlayListener " + name + " in the GameRegistry because it is neither a Block, Item or ItemStack!"); } } else { postponedOredictEntries.add(new Tuple<>(thing, name)); } } else if (name == null) { Log.warn("Attempted to registerGameOverlayListener Object to OreDict with null OreDict Name!"); } else { Log.warn("Cannot registerGameOverlayListener null Object(" + name + ") to the OreDictionary!"); } return thing; } public IOreDictNamed registerOreDict(@Nonnull IOreDictNamed thing) { if (thing.getOreDictName() != null) { registerOreDict(thing, thing.getOreDictName()); } else { Log.warn("Attempted to registerGameOverlayListener IOreDictNamed to OreDictionary with null OreDict Name!"); } return thing; } @SideOnly(Side.CLIENT) public void registerRenderer(@Nonnull T thing) { if (thing instanceof Item) { itemRenderables.add((Item) thing); } else { Item item = thingItemMap.get(thing); if (item != null) { itemRenderables.add(item); } else if (thing instanceof Renderable) { addToRenderable(thing); } else { Log.warn("Could no registerGameOverlayListener Renderer for non-item and non-renderable"); } } } @SideOnly(Side.CLIENT) public void registerItemRenderer(@Nonnull Item thing) { if (thing.getHasSubtypes()) { Log.trace("Registering item with subtypes " + getName(thing)); CreativeTabs tabs; if (thing instanceof IRenderSubTabProvider) { tabs = ((IRenderSubTabProvider) thing).getSubTab(); } else { tabs = CTabs.TAB_MAIN; } NonNullList<ItemStack> itemStacks = NonNullList.create(); thing.getSubItems(tabs, itemStacks); for (ItemStack stack : itemStacks) { registerItemRenderer(stack.getItem(), stack.getMetadata()); } } else { registerItemRenderer(thing, 0); } } @SideOnly(Side.CLIENT) protected void registerItemRenderer(@Nonnull Item item, @Nonnegative int meta) { if (item.getRegistryName() != null) { ResourceLocation location = item.getRegistryName(); if (item instanceof IRenderable && ((IRenderable) item).getLocation() != null) { location = ((IRenderable) item).getLocation(); } String path = StringHelper.createResourceLocation(MODID, location.getResourcePath()); if (!(item instanceof IRenderable) || ((IRenderable) item).registerRenderer()) { ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(path, "inventory")); Log.trace("Registered renderer for " + getName(item) + " with meta " + meta + "."); } } else { Log.warn("Could not registerItemRenderer for " + getName(item) + " because Registry Name hasn't been set!"); } } public void addToRenderable(T t) { if (t instanceof IRenderable) { if (thingItemMap.containsKey(t) && thingItemMap.get(t) != null) { itemRenderables.add(thingItemMap.get(t)); } else { renderable.add(t); } } } protected void addToItemMap(T thing, Item item) { if (!thingItemMap.containsKey(thing)) { thingItemMap.put(thing, item); } } public Collection<Item> getRegisteredItems() { return thingItemMap.values(); } } As you can see: I Split up Code in multiple Files and don't create obsolete Arrays (use your varargs solution it is pretty elegant as long as you DON'T create a seperate array). I know that most peoble will call this overconstructed, but I am not that far with the Mod I copied that from, so there aren't enough Blocks to justify that much Code yet. In your case it would be more suitable to use a solution with less and much simpler Code, which doesn' declare all of your Variables as final
  4. (And one more thing, if you want to create your Instance during Block registration, you have to make your Field non-final, like I suggest you'd do with most-variables in this class too. Not every variable needs to be a constant.)
  5. You register the Wrong Element... Currentl you are Creating a new Instance of your AmethystOre when you register your Blocks. But when you then register your Items you access your static reference. Just Change your Block registration, so that you store your Instance in your static final variable and then register that. (I hope you know, that your Java Code is a Mess, so I won't point all your mistakes out to you (that would take too much time)) (Pls learn too Code...)
  6. I'm quite experinced with java, but like theSieben just pointed out to me, programming since 2 (am) doesn't do anything good to your mind... But let's take a look anyway.
  7. Sry... I'm such an idiot at Times... I used auto completion, and tried to access the private Constructor which naturally wouldn't work (and I didn't try to add a value to it, but rather tried to find an alternative to that...) Sry sry sry sry sry. (I have been looking for that for about the last 1.5h ) (I think programming scince 2 O'clock (am) doesn't do anything good to your mind...) Edit: Is it possible to close Stupid Topics like this?
  8. To clarify: you should understand that, before asking such a question... https://mcforge.readthedocs.io/en/latest/concepts/sides/
  9. So if you want to get help, could you just provide some Information?
  10. Helllo, I have a Method which requires converting a Map<INBTSerializable,Boolean> into NBTTagLists, but it looks like NBTTagByte (and similiar primitves) are package-private classes and there fore cannot be used. More specifically here is some simplified code of what I'm doing: private NBTTagList[] getConditionValueResources() { NBTTagList[] locations = {new NBTTagList(), new NBTTagList() }; for (Map.Entry<ISpellCondition,Boolean> condition : conditions.entrySet()) { locations[0].appendTag(condition.getKey().serializeNBT()); NBTTagCompound compound = new NBTTagCompound(); compound.setBoolean(KEY_BOOLEAN_VALUE,condition.getValue()); locations[1].appendTag(compound); } return locations; } and what I'm looking for would be sth like: private NBTTagList[] getConditionValueResources() { NBTTagList[] locations = {new NBTTagList(), new NBTTagList() }; for (Map.Entry<ISpellCondition,Boolean> condition : conditions.entrySet()) { locations[0].appendTag(new NBTTagBoolean(condition.getValue()); locations[1].appendTag(compound); } return locations; } This piece of code would allow for far less Overhead (an NBTTagCompound is contains an entire Map, although i only need on byte!) and I would omit this stupid converting NBTTagCompound and back... (Note: I know, that there is no NBTTagBoolean Class, but that NBTTagByte is used instead. This would make it far easier too... But it is not public... )
  11. One thing, could you please post the stacktrace instead of one single line? Because it seems odd to get an NullPointer from Forge itself, it's probably some bug in your code inside the given Method
  12. Like Changing the Text Color? That would result in sth like that...: tooltip.add(TextFormatting.GRAY + I18n.format("wand.efficiency") + ": " + (getEfficiency() >= boundDefinition.getPerfectEfficiencyBorder() ? TextFormatting.GOLD : (getEfficiency() >= definition.getPerfectEfficiencyBorder() ? TextFormatting.AQUA : TextFormatting.DARK_BLUE)) + Math.round(getEfficiency()) + "%"); More specifically, im referring to the TextFormatting.sth
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.