so I'm trying to do broadcast command but it sends out the command name I don't know why? if you have any idea why and how to solve this problem
package ml.harrytubestudios.helloworld.commands;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import ml.harrytubestudios.helloworld.main;
public class bro implements CommandExecutor {
private main plugins;
#Override
public boolean onCommand(CommandSender sender, Command cmd, String no, String[] args) {
Bukkit.broadcastMessage(no);
return false;
}
}
Your problems here are twofold. If you look at the documentation for CommandExecutor; it's the same in Spigot as in Bukkit, you'll see that it says for onCommand:
If false is returned, then the "usage" plugin.yml entry for this command (if defined) will be sent to the player.
Because you're returning false, you're saying that the command wasn't entered correctly and the usage string should be sent to the CommandSender. You should return true if the command executed successfully.
However, you should still see your broadcastMessage, which you are. This is explained again in the documentation as it says for the 3rd argument (label):
Alias of the command which was used
This means that you are broadcasting the alias of the command (your no parameter) that the CommandSender used, rather than their arguments, which is what I presume you're after.
In order to get the arguments of the command used, you'll want the args parameter which is an array of Strings. You'll probably want to format this into one string for use with your broadcast; for which there are different solutions.
I found out the answer its that string is the command and the args is the arguments after the command so
package ga.harrytubestudios.helloworld.commands;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import ga.harrytubestudios.helloworld.main;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
public class shopcommand implements CommandExecutor {
private main pluign;
#Override
public boolean onCommand(CommandSender Sender, Command smd, String label, String[] args) {
Player p = (Player)Sender;
p.sendmessgae(args[0])
}
Related
Sa as the title says, I have a plugin that detects swear words. What it does now is that it sends a message to the player. But I also want it to execute a command that is already in the game or from another plugin. I'm not sure how to do this. How is that done?
Here is my current code:
package
import com.beam.sweardetector.SwearDetector;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
public class EventsClass implements Listener {
SwearDetector plugin = SwearDetector.getPlugin(SwearDetector.class);
#EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent event) {
Player player = event.getPlayer();
player.sendMessage(ChatColor.GOLD + "This server is running AntiSwear v1.0 by BeamCRASH");
}
#EventHandler
public void chatevent (AsyncPlayerChatEvent event) {
for(String s: event.getMessage().split(" ")) {
if(plugin.getConfig().getStringList("swears").contains(s)) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.DARK_RED + "§lSwearing is not allowed on this server!");
}
}
}
}
Any help will be appreciated.
Thank you!
You can execute command as player :
event.getPlayer().performCommand("mycommand");
But, it will not if the player don't have enough permission.
To run command as console, use this :
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mycommand");
Also, you don't need the / at the beginning of command because it's only to say that it's a command and it can change between players.
Warn: you should run those code in sync. If you are async (specially from AsyncEvent), you should use this code :
Bukkit.getScheduler().runTask(plugin, () -> {
// my code
player.performCommand("kill");
});
Alright, So I had this problem on a project I've started in bukkit recently. As you will see I defined the head and everything, I set the owner of the head and applied it. But when I load in-game it shows normal Steve head! What i want it to do is when i execute the command "spawnmnz" It will spawn a minion with the sender/player's (In this case) head!
package me.frostgamersa;
import net.minecraft.server.v1_8_R3.ItemSkull;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.SkullType;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.java.JavaPlugin;
public class NewMinion extends JavaPlugin {
String minion_name = "§3Minion §bSpawn §fEgg §8- §a[Spawned]";
#Override
public void onEnable() {
}
#Override
public void onDisable() {
}
#SuppressWarnings("deprecation")
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label,
String[] args) {
Player player = (Player) sender;
if (cmd.getName().equalsIgnoreCase("spawnmnz")){
World world = player.getWorld();
Location loc = player.getLocation();
ItemStack p_skull = new ItemStack(Material.SKULL_ITEM, 1, (short)
SkullType.PLAYER.ordinal());
SkullMeta sm = (SkullMeta) p_skull.getItemMeta();
sm.setOwner(player.getName());
p_skull.setItemMeta(sm);
Zombie minion = (Zombie) world.spawn(loc, Zombie.class);
minion.setBaby(true);
minion.setCustomName(minion_name);
minion.setCustomNameVisible(true);
minion.getEquipment().setHelmet(p_skull);
return true;
}
return false;
}
}
Get the skinvalue from the players profile and then set it on the skull item usin reflection, if it is not clear to you how to do this i will provide you a api for that ;)
Does the server have an internet connection? It has to download the skin from the Mojang servers. Because of that, it might also just take a moment for the skin to appear on the head.
The import for the ItemSkull also seems weird, what server version do you use?
I am coding a Minecraft Plugin using of course Java for Minecraft 1.12
I have the latest version of Java and eclipse also the Bukkit Api.
This is the error I am getting:
String[] r *=* ("Spamming", "test1", "test2,", "test3", "Test34");
for (String reason : r)
The = is under red line with this error:
Syntax error on token "=", Name expected after this tokenReasonGUI.java /WarningSystem/src/listener line 28 Java Problem
full code:
package listeners;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.SkullType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import me.OctoberTroy.WarningSystem.MainClass1;
public class ReasonGUI implements Listener{
MainClass1 main = MainClass1.getPlugin(MainClass1.class);
public Inventory rinv = Bukkit.createInventory(null, 9, "Select a reason to warn the player!");
public ReasonGUI(Player player){
if (player == null){
return;
}
String[] r = ("Spamming", "test1", "test2,", "test3", "Test34");
for (String reason : r);
Java uses braces to initialize an array.
String[] r = {"Spamming", "test1", "test2,", "test3", "Test34"};
Furthermore, although your program will compile, your for loop has no implementation. You cycle through each string in your newly declared array, but do nothing with it. Put the implementation for it as such:
for (String reason : r){
// IMPLEMENTATION GOES HERE
}
In Java you use curly braces {} for arrays, not parenthesis ().
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 have the following question: I am trying to execute the usConstitution wordcram example (code follows) and if provided as is the code executes in eclipse, the applet starts and the word cloud is created. (code follows)
import processing.core.*;
//import processing.xml.*;
import wordcram.*;
import wordcram.text.*;
import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class usConstitution extends PApplet {
/*
US Constitution text from http://www.usconstitution.net/const.txt
Liberation Serif font from RedHat: https://www.redhat.com/promo/fonts/
*/
WordCram wordCram;
public void setup() {
size(800, 600);
background(255);
colorMode(HSB);
initWordCram();
}
public void initWordCram() {
wordCram = new WordCram(this)
.fromTextFile("http://www.usconstitution.net/const.txt")
.withFont(createFont("https://www.redhat.com/promo/fonts/", 1))
.sizedByWeight(10, 90)
.withColors(color(0, 250, 200), color(30), color(170, 230, 200));
}
public void draw() {
if (wordCram.hasMore()) {
wordCram.drawNext();
}
}
public void mouseClicked() {
background(255);
initWordCram();
}
static public void main(String args[]) {
PApplet.main(new String[] { "--bgcolor=#ECE9D8", "usConstitution" });
}
}
My problem is the following:
I want to pass through main (which is the only static class) an argument so as to call the usConstitution.class from another class providing whichever valid filename I want in order to produce its word cloud. So how do I do that? I tried calling usConstitution.main providing some args but when I try to simply print the string I just passed to main (just to check if it is passed) I get nothing on the screen. So the question is How can I pass an argument to this code to customize .fromTextFile inside initWordCram ?
Thank you a lot!
from: https://wordcram.wordpress.com/2010/09/09/get-acquainted-with-wordcram/ :
Daniel Bernier says:
June 11, 2013 at 1:13 am
You can’t pass command-line args directly to WordCram, because it has no executable.
But you can make an executable wrapper (base it on the IDE examples that come with WordCram), and it can read command-line args & pass them to WordCram as needed.
FYI, it’ll still pop up an Applet somewhere – AFAIK, you can’t really run Processing “headless.” But that’s usually only a concern if you’re trying to run on a server.