I've tried to modify minecraft by adding a new item called "uranium". Therefore I created the class "Trauma.java" in the main package and a few other classes listed below.
All packages and classes:
Package Explorer
Trauma.java
package main;
import items.ItemUranium;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import proxy.ServerProxy;
#Mod(modid = Trauma.MODID)
public class Trauma {
public static final String MODID = "Trauma";
#SidedProxy(clientSide = "proxy.ClientProxy", serverSide = "proxy.ServerProxy")
public static ServerProxy proxy;
public static ItemUranium uranium = new ItemUranium();
#EventHandler
public void preInit(FMLPreInitializationEvent event) {
GameRegistry.register(uranium);
}
#EventHandler
public void init(FMLInitializationEvent event) {
proxy.registerClientStuff();
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
}
BasicItem.java
package items;
import net.minecraft.item.Item;
public class BasicItem extends Item {
public BasicItem(String name) {
setUnlocalizedName(name);
setRegistryName(name);
}
}
ItemUranium.java
package items;
public class ItemUranium extends BasicItem {
public ItemUranium() {
super("uranium");
}
}
ClientProxy.java
package proxy;
import items.BasicItem;
import main.Trauma;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
public class ClientProxy extends ServerProxy {
#Override
public void registerClientStuff () {
registerItemModel(Trauma.uranium);
}
public static void registerItemModel(BasicItem item) {
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Trauma.MODID + ":" + item.getRegistryName(), "inventory"));
}
}
ServerProxy.java
package proxy;
public class ServerProxy {
public void registerClientStuff() {}
}
uranium.json
{
"parent": "item/generated",
"textures": {
"layer0": "Trauma:items/uranium"
}
}
uranium.png
ingame
Also I don't know why the item in inventory isn't called uranium...
I spent two hours on fixing the problem and it didn't help so it would be really nice if somebody of you may help me.
Thanks :)
Don't use the Model Mesher:
The model mesher is Vanilla (Mojang) code and using it correctly has always been finicky and unreliable, failing if you called it too early and failing if you called it too late. So Forge added the ModelLoader class to resolve that problem.
Replace this line:
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(...)
With this line:
ModelLoader.setCustomModelResourceLocation(...)
The ... contents are identical.
Second, depending on what version of Minecraft you're using, you should...:
Stop using GameRegistry.Register
Instead use the RegistryEvent.Register<T> events (where <T> will be <Block> to register blocks, <Item> to register items, etc)
Register your models in the ModelRegistryEvent and no where else.
This event is #SideOnly(CLIENT) and can be subscribed to in your client proxy, avoiding the need to forward references through your proxy class. Eg. I do it like this, where lines 197-199 is the most common scenario needed, where the array is populated during the item registration event. The rest of that method handles the custom state mappers and custom mesh definitions that are used by only a handful of items/blocks and not relevant here.
Include your Mod ID in your unlocalized name. The best way to do this would be setUnlocalizedName(getRegistryName().toString());
See also the Forge documentation on events.
Related
I'm trying to make my bot welcome someone whenever someone joins but I can't seem to get it to work. For example(this will appear in an embed by the way):
#Jason joined. You must construct additional pylons.
Can someone help me edit my code so it works please?
Here's my Main code:
import Events.*;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import javax.security.auth.login.LoginException;
public class Main {
public static void main(String[] args) throws LoginException {
JDABuilder jda = JDABuilder.createDefault("I imported key here");
jda.setActivity(Activity.watching("baldness"));
jda.addEventListeners(new Help());
jda.addEventListeners(new PingPong());
jda.addEventListeners(new Clear());
jda.addEventListeners(new Welcome());
jda.build();
}
}
and Here's my Welcome code:
package Events;
import java.util.Random;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Welcome extends ListenerAdapter {
public class GuildMemberJoin extends ListenerAdapter {
String[] messages = {
"[member] joined. You must construct additional pylons.",
"Never gonna give [member] up. Never let [member] down!",
"Hey! Listen! [member] has joined!",
"Ha! [member] has joined! You activated my trap card!",
"We've been expecting you, [member].",
"It's dangerous to go alone, take [member]!",
"Swoooosh. [member] just landed.",
"Brace yourselves. [member] just joined the server.",
"A wild [member] appeared."
};
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
Random rand = new Random();
int number = rand.nextInt(messages.length);
EmbedBuilder join = new EmbedBuilder();
join.setColor(0x66d8ff);
join.setDescription(messages[number].replace("[member]", event.getMember().getAsMention()));
event.getGuild().getDefaultChannel().sendMessage(join.build()).queue();
}
}
}
The documentation fo GuildMemberJoinEvent clearly states:
Requirements
This event requires the GUILD_MEMBERS intent to be enabled.
createDefault(String) and createLight(String) disable this by default!
So you must enable the intent. Read more in the wiki guide Gateway Intents and Member Cache Policy
Additionally, you have a nested class for no reason which means you register the enclosing class as a listener but not the nested class that actually implements it.
Better:
public class Welcome extends ListenerAdapter {
#Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
...
}
}
I want to build a custom renderer for some of my grids columns to hide the text if the user doesn't have the right to read it. It's still important that the data is accessible even if the user is not able to read it.
So I wrote a custom renderer which looks like this:
package <package>.util.renderer;
import com.vaadin.client.renderers.Renderer;
import com.vaadin.client.widget.grid.RendererCellReference;
import <package>.util.CustomSecurityConstants;
import <package>.BaseUI;
public class BlockedStringRendererClient implements Renderer<String> {
private boolean canReadBlocked = BaseUI.getCurrentPrincipal().get().getAuthorities().contains(CustomSecurityConstants.READ_PERMISSION_BLOCKED);
#Override
public void render(RendererCellReference rendererCellReference, String s) {
if (canReadBlocked) {
rendererCellReference.getElement().setInnerText(s);
} else {
rendererCellReference.getElement().setInnerText("");
}
}
}
Then I wrote the server side of the renderer, following this tutorial https://vaadin.com/docs/-/part/framework/clientsidewidgets/clientsidewidgets-grid.html
package <package>.util.renderer;
import com.vaadin.ui.Grid;
public class BlockedStringRendererServer extends Grid.AbstractRenderer<String> {
public BlockedStringRendererServer() {
super(String.class);
}
}
And finally the connector to connect these components:
package <package>.util.renderer;
import com.vaadin.client.connectors.AbstractRendererConnector;
import com.vaadin.shared.ui.Connect;
#Connect(BlockedStringRendererServer.class)
public class BlockedStringRendererConnector extends AbstractRendererConnector<String> {
#Override
public BlockedStringRendererClient getRenderer() {
return (BlockedStringRendererClient) super.getRenderer();
}
}
But now when I try to use the connector like follows:
grunddatenGrid.getColumn("name").setRenderer(new BlockedStringRendererServer());
The grid doesn't show any columns that contains strings anymore.
I really don't know what I'm doing wrong but I think it might has to do with the Connector-Annotation not working as expected because when I try to debug the client side logic it doesn't even get called.
Can somebody point me to what steps I'm missing?
Kind regards,
Fabian
I am new to creating minecraft plugins, however not new to programming, I am following a tutorial very thoroughly, the video has good ratings so it is trusted, when watching the video the guy has no problems what so ever (Youtube video on developing minecraft plugins) , so I did some research into solutions but always the line through the code.
Eclipse gives me the option for: #SuppressWarnings("deprecation") which allows the code to be used still but I would rather have no need of that usage.
Basically my question is why is there the need of the line going through the code and how do I find a solution to get rid of it.
Main class:
package com.jc1;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Core extends JavaPlugin
{
public Permission pPermission = new Permission("playerAbilities.allowed");
#Override
public void onEnable()
{
new BlockListener(this);
PluginManager pm = getServer().getPluginManager();
pm.addPermission(pPermission);
}
#Override
public void onDisable()
{
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("giveitems") && sender instanceof Player)
{
Player p = (Player) sender;
if(p.hasPermission("playerAbilities.allowed"))
{
p.setItemInHand(new ItemStack(Material.DIAMOND_BOOTS));
}
return true;
}
return false;
}
}
Secondary class:
package com.jc1;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class BlockListener implements Listener
{
public BlockListener(Core plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void onBlockPlace(BlockPlaceEvent e)
{
Player p = e.getPlayer();
if(!p.hasPermission("playerAbilities.allowed"))
{
e.setCancelled(true);
}
}
}
The method is deprecated, meaning that it is not recommended to be used anymore and is most likely replaced with another method.
Methods that are deprecated may still work as intended.
A simple search for the method reveals (this) documentation, stating:
players can duel wield now use the methods for the specific hand instead
which referes to the #see references:
getItemInMainHand() and getItemInOffHand().
Use this:
player.getInventory().getItemInMainHand()
Instead of:
player.getItemInHand()
Hope this helps! :D
I am trying to add a texture to an item, yet the texture just doesn't appear. I have the texture made, and in the right file directory, but while in game it doesn't display. Thus, I think it's an error in my code.
For the whole class file, see below:
package Moonstone;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.EnumHelper;
#Mod(modid = "ms", name = "Moonstone", version = "1.0")
public class MoonstoneMain {
public static Item moonstone;
#EventHandler
public void preInit(FMLPreInitializationEvent event) {
//All init
moonstone = new Moonstone().setUnlocalizedName("Moonstone").setTextureName("moonstone").setMaxStac kSize(64);
GameRegistry.registerItem(moonstone, moonstone.getUnlocalizedName().substring(5));
}
#EventHandler
public void init(FMLInitializationEvent event) {
//Proxy, TileEntity, entity, GUI and packet registering
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
public static CreativeTabs tabMoonstone = new CreativeTabs("tabMoonstone"){
#Override
public Item getTabIconItem(){
return new ItemStack(Items.stick).getItem();
}
};
}
For just the item, look below-
moonstone = new Moonstone().setUnlocalizedName("Moonstone").setTextureName("moonstone").setMaxStackSize(64);// I have tried with ms:moonstone and without, both don't work.
GameRegistry.registerItem(moonstone, moonstone.getUnlocalizedName().substring(5));
Recommended changes but not necessary:
First:
When registering the item you should remove the .substring(5),
having this in will name the item "Moons" instead of "Moonstone".
Second:
unlocalized names should always be lowercase and should be formatted
as modid_itemname
Third:
Package names should be lowercase, package moonstone
Fourth:
Your should make a Refstrings.java file and put the modid, version and name in it
package Moonstone
public RefStrings{
public static String NAME = "Moonstone";
public static String MODID = "ms";
public static String VERSION = "1.0";
}
Necessary changes:
Your setTextureName should be passed "ms:moonstone"
You didn't post your folder structure but it should look like this:
src/main/java/Moonstone/MoonstoneMain.java
src/main/resources/assests/ms/textures/items/moonstone.png
it is possible that some of the recommend changes will be what fixes the problem, minecraft can be a bit finicky with naming.
So I'm creating a mod in Minecraft. It registers the sword but doesn't appear in the game. What can I do to make it appear in he game? Thanks in advance.
package com.ethan.main;
import net.minecraft.item.Item;
import net.minecraft.item.Item.ToolMaterial;
import net.minecraft.item.ItemSword;
import net.minecraftforge.common.util.EnumHelper;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
#Mod(modid = "elemental_swords", version = "1.0 Alpha", name = "Elemental Swords Mod")
public class ElementalSwords {
public static final String modid = "elemental_swords";
public static Item lightningsword;
public static ToolMaterial Element = EnumHelper.addToolMaterial("Element", 9, 1378, 1000, 10, 5);
public void preInit(FMLPreInitializationEvent event){
lightningsword = new LightningSword(Element, "lightningsword");
GameRegistry.registerItem(lightningsword, "Lightning Sword");
}
public void init(FMLInitializationEvent event){
}
}
Here is the Item Class:
package com.ethan.main;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.Item.ToolMaterial;
import net.minecraft.item.ItemSword;
public class LightningSword extends ItemSword{
public LightningSword(ToolMaterial material, String name) {
super(material);
setUnlocalizedName(ElementalSwords.modid + "_" +name);
setTextureName(ElementalSwords.modid + ":" + name);
}
}
Your problem is that you did not put the #EventHandler annotation on the preInit and init methods. Here's how your code should look like:
#EventHandler //Important
public void preInit(FMLPreInitializationEvent event) {
//Read configs here.
}
#EventHandler
public void init(FMLInitializationEvent event) {
//Register blocks and items here.
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
//Have mod integeration here.
}
Also, when registering blocks and items, avoid spaces.
Example: GameRegistry.registerItem(lightningsword, "Lightning Sword");
should be
GameRegistry.registerItem(lightningsword, "lightningSword");
Also, as pointed out by #FerretBitStudios, you need to set the creative tab to show in. If you do not, the only way to get the item is through NEI or the /give command.
To get your item to show up in your creative inventory, use lightningsword.setCreativeTab(CreativeTabs.tabCombat) or another creative tab. Also, I know you haven't hit this roadblock yet, but I assume you're using forge for 1.8, in which case settexturename() won't work. 1.8 uses a json model system for rendering items. There's a great tutorial on it over here. Hope it helps :)