I want to create a custom Skeleton that has a name, more health and holds other custom items.
I can add a name and setHealth(), but I can't setMaxHealth() and also setting items and armor just won't work.
Thanks for helping, here is my code:
Player p = (Player) sender;
WorldServer world = ((CraftWorld)p.getWorld()).getHandle();
Location loc = p.getLocation();
if (args.length > 0) {
if (args[0].equalsIgnoreCase("define")) {
//get worldedit selection
if (getWorldEdit().getSelection(p) == null) {
p.sendMessage(title + "Please select a region with WorldEdit");
return false;
}
s = getWorldEdit().getSelection(p);
Location min = s.getMinimumPoint();
Location max = s.getMaximumPoint();
//boss mob creation
EntitySkeleton boss = new EntitySkeleton(world);
boss.setHealth(400);
boss.setCustomName("§4§lDAFT BOSS");
boss.setCustomNameVisible(true);
ItemStack weapon = new ItemStack(Material.DIAMOND_SWORD);
weapon.setDurability((short) 0);
weapon.addEnchantment(Enchantment.DAMAGE_ALL, 5);
weapon.addUnsafeEnchantment(Enchantment.KNOCKBACK, 2);
boss.setLocation(max);
world.addEntity(boss);
}
That can be obtained using the Attributable interface, as said in this thread from Spigot.
Example:
For 1.9 and over:
Entity boss;
Attributable bossAttributable = (Attributable) boss;
AttributeInstance ai = bossAttributable.getAttribute(Attribute.GENERIC_MAX_HEALTH);
ai.setValue(400.0);
For 1.8.8 and lower, this must be done another way:
Entity boss;
Damageable bossDamageable = (Damageable) boss;
bossDamageable.setMaxHealth(400.0);
Related
I have made plugin that have 2 kits first with name Iron and the other with the name DProtect
so i want them to give items and i made everything and i made a cool down the problem is when i type /kit Iron or /kit DProtect it doesn't work i tried to do a lot of things and doesn't work this is the code can i have some help ?
package Rivals.iSryMan.Kits.commands;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
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 org.bukkit.inventory.meta.ItemMeta;
import Rivals.iSryMan.Kits.Main;
public class Kit implements CommandExecutor{
private HashMap<UUID,Long> ironcooldown = new HashMap<UUID,Long>();
private int ironcooldowntime = 300;
private HashMap<UUID,Long> DProtectcooldown = new HashMap<UUID,Long>();
private int DProtectcooldowntime = 5259487;
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(sender instanceof Player) {
Player p = (Player)sender;
if(args.length == 1) {
//Kit Iron
if(args[0].equals("Iron")) {
if(ironcooldown.containsKey(p.getUniqueId())) {
long ironsecondsleft = ( (ironcooldown.get(p.getUniqueId())/ 1000) + ironcooldowntime) - (System.currentTimeMillis() / 1000);
if(ironsecondsleft > 0) {
p.sendMessage(Main.Color(Main.prefix + "You must wait " + ironsecondsleft + " before you take that kit again!"));
}else if(ironsecondsleft == 0) {
final ItemStack ironhelmet = new ItemStack(Material.IRON_HELMET);
final ItemStack ironchestplate = new ItemStack(Material.IRON_CHESTPLATE);
final ItemStack ironleggings = new ItemStack(Material.IRON_LEGGINGS);
final ItemStack ironboots = new ItemStack(Material.IRON_BOOTS);
p.getInventory().addItem(ironhelmet);
p.getInventory().addItem(ironchestplate);
p.getInventory().addItem(ironleggings);
p.getInventory().addItem(ironboots);
p.sendMessage(Main.Color(Main.prefix + " You've got your kit, Enjoy!"));
}
}
//Kit Iron
//Kit DProtect
} else if (args[0].equals("DProtect")) {
if(DProtectcooldown.containsKey(p.getUniqueId())) {
long DProtectsecondsleft = ( (DProtectcooldown.get(p.getUniqueId()) / 1000) + DProtectcooldowntime) - (System.currentTimeMillis() / 1000);
if(DProtectsecondsleft > 0) {
p.sendMessage(Main.Color(Main.prefix + "You must wait " + DProtectsecondsleft + " before you take that kit again!"));
}else if(DProtectcooldowntime == 0) {
final ItemStack dphelmet = new ItemStack(Material.IRON_HELMET);
final ItemMeta dphelmetmeta = dphelmet.getItemMeta();
final ItemStack dpchestplate = new ItemStack(Material.IRON_CHESTPLATE);
final ItemMeta dpchestplatemetaa = dphelmet.getItemMeta();
final ItemStack dpleggings = new ItemStack(Material.IRON_LEGGINGS);
final ItemMeta dpleggingsmeta = dphelmet.getItemMeta();
final ItemStack dpboots = new ItemStack(Material.IRON_BOOTS);
final ItemMeta dpbootsmeta = dphelmet.getItemMeta();
ArrayList<String> dplore = new ArrayList<String>();
dplore.add(Main.Color("&bAuthentic Protection 4 Armor"));
//Set Meta
dphelmet.setItemMeta(dphelmetmeta);
dpchestplate.setItemMeta(dpchestplatemetaa);
dpleggings.setItemMeta(dpleggingsmeta);
dpboots.setItemMeta(dpbootsmeta);
//Set Meta
//Set Lore
dphelmetmeta.setLore(dplore);
dpchestplatemetaa.setLore(dplore);
dpleggingsmeta.setLore(dplore);
dpbootsmeta.setLore(dplore);
//Set Lore
//Adding Kit
p.getInventory().addItem(dphelmet);
p.getInventory().addItem(dpchestplate);
p.getInventory().addItem(dpleggings);
p.getInventory().addItem(dpboots);
//Adding Kit
p.sendMessage(Main.Color(Main.prefix + " You've got your kit, Enjoy!"));
}
}
//Kit DProtect
}
return true;
}
}
return false;
}
}
Have you made sure:
Your command is registered in onEnable() and plugin.yml?
You type specifically 'Iron' with a capital I? You should allow for different cases, using equalsIgnoreCase()
This is probably the issue. Your HashMap does not contain the UUID of the CommandExecutor at first, therefore it never gets passed the ironcooldown.containsKey(p.getUniqueId()). I would recommend checking if they are in it. If they are, check if the time is 0, otherwise add them after the code has been executed.
Let me know if this works.
Also, you should fix the indentation of your code - it confused me at first and it may confuse you in the future. This is not a big deal, however.
Although Slinthn answered your questions, I thought I'd add a bit more of an explanation.
You declare a private HashMap for both kits cooldown's: ironcooldown and DProtectcooldown.
You check within your onCommand if the player executing the command is in that HashMap in your condition if(ironcooldown.containsKey(p.getUniqueId())), however, that condition will never be true because the player's UUID is never added to the HashMap.
It looks to me like you want to do something like the following (half pseudo-code)
if(!(sender instanceof Player))
return true;
if(args.length == 0)
return true;
Player p = (Player) sender;
if(args[0].equalsIgnoreCase("iron")){
if(ironcooldown.containsKey(p.getUniqueId())){
//Do check if they are still on cooldown
//... if so, return here
}
ironcooldown.put(p.getUniqueId(), System.currentTimeMillis());
givePlayerKit(Kits.IRON, p); //made up function that takes Enum & Player
}
//etc
It seems that you haven't added the player to the HashMap so it will return false so you might want to make your HashMaps public and static and inside your PlayerJoinEvent (if you have one) add
Kit.ironcooldown.put(p.getUniqueId(), 0);
or if you don't have a PlayerJoinEvent and/or you don't want to do that, you can put before your args check
if (!ironcooldown.containsKey(p.getUniqueId()) {
ironcooldown.put(p.getUniqueId(), 0);
}
Do the same for Dprotectcooldown.
Hope this helped!
I've been trying to create a custom inventory and everything seems fine but in-game when I try to right-click the item that opens the inventory, the inventory doesn't open.
Inventory playerInfoInv = plugin.getServer().createInventory(null, 27, ChatColor.GOLD + "Player Info");
p.openInventory(playerInfoInv);
}
#EventHandler
public void onInteract(PlayerInteractEvent e) {
Player p = e.getPlayer();
Material getItemInHand = e.getItem().getType();
Action a = e.getAction();
if (getItemInHand.equals(SKULL_ITEM)) {
if (a.equals(Action.LEFT_CLICK_AIR))
playerInfoInventory(p);
}
}
This is the skull item meta if it might impact this:
//Player skull
ItemStack pSkull = new ItemStack(SKULL_ITEM,1,(short) SkullType.PLAYER.ordinal());
SkullMeta pMeta = (SkullMeta) pSkull.getItemMeta();
pMeta.setOwner(p.getName());
pMeta.setDisplayName(ChatColor.BLUE + "Player Info");
ArrayList<String> pSkullLore = new ArrayList<String>();
pSkullLore.add(ChatColor.WHITE + "Show Player Stats");
pMeta.setLore(pSkullLore);
pMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
pSkull.setItemMeta(pMeta);
p.getInventory().setItem(0, pSkull);
At first: e.getItem() can be null. So maybe you could get an NullPointer.
The second: ItemStack#getType() returns Material. And you are checking for SKULL_ITEM not Material.SKULL_ITEM (same mistake you made at initializing your variable pSkull).
The Third: Check if your Listener is Registered.
I am trying to make my plugin monitor kills and hp of a player but I am currently getting issues with both health and kills with the same error.
I have even tried using null selection to check if it is null and not use it but that wouldn't work as I need the information to show.
I basically need to set the players hp and name as the scoreboard and the kills as the score. (Thank you in advance).
This sets the players kills when joining:
#EventHandler
public void onPlayerLogin(PlayerJoinEvent e) {
String player = e.getPlayer().getName();
if(!PlayerHandler.GPlayed.containsKey(player)){
PlayerHandler.GPlayed.put(player, 0);
}
if(!PlayerHandler.kills.containsKey(player)){
PlayerHandler.kills.put(player, 0);
}
if(!PlayerHandler.HRound.containsKey(player)){
PlayerHandler.HRound.put(player, 0);
}
}
And here is a debug I tried creating but the same error still shoots with the relevant checks all being correct.
#SuppressWarnings("deprecation")
public void run(String arena){
for(String key : Handler.playerMap.keySet()){
if (Handler.playerMap.get(key).contains(arena)){
Player pt = Bukkit.getPlayer("Nubzz");
String p = pt.getDisplayName();
Player nubzz = Bukkit.getPlayer("Nubzz");
nubzz.sendMessage(key);
nubzz.sendMessage(pt + "");
nubzz.sendMessage(p);
ScoreboardManager manager = Bukkit.getScoreboardManager();
Scoreboard board = manager.getNewScoreboard();
Objective objective = board.registerNewObjective("test", "dummy");
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
objective.setDisplayName(ChatColor.AQUA + "Wave: " );
Score score = objective.getScore(ChatColor.GREEN + "" + pt.getHealth() + ChatColor.WHITE + p + ChatColor.AQUA);
int kills = PlayerHandler.kills.get(p);
score.setScore(kills);
int pcheck = 0;
This shows the debug working and showing the player has kills (Maybe I'm checking the wrong variables values):
https://gyazo.com/35a83b11a1fb384789a15ae54116248d
I am attempting to get the items and some of the related information from a Purchase Order with SuiteTalk. I am able to get the desired Purchase Orders with TransactionSearch using the following in Scala:
val transactionSearch = new TransactionSearch
val search = new TransactionSearchBasic
...
search.setLastModifiedDate(searchLastModified) //Gets POs modified in the last 10 minutes
transactionSearch.setBasic(search)
val result = port.search(transactionSearch)
I am able to cast each result to a record as an instance of the PurchaseOrder class.
if (result.getStatus().isIsSuccess()) {
println("Transactions: " + result.getTotalRecords)
for (i <- 0 until result.getTotalRecords) {
try {
val record = result.getRecordList.getRecord.get(i).asInstanceOf[PurchaseOrder]
record.get<...>
}
catch {...}
}
}
From here I am able to use the getters to access the individual fields, except for the ItemList.
I can see in the NetSuite web interface that there are items attached to the Purchase Orders. However using getItemList on the result record is always returning a null response.
Any thoughts?
I think you have not used search preferences and that is why you are not able to fetch purchase order line items. You will have to use following search preferences in your code -
SearchPreferences prefrence = new SearchPreferences();
prefrence.bodyFieldsOnly = false;
_service.searchPreferences = prefrence;
Following is working example using above preferences -
private void SearchPurchaseOrderByID(string strPurchaseOrderId)
{
TransactionSearch tranSearch = new TransactionSearch();
TransactionSearchBasic tranSearchBasic = new TransactionSearchBasic();
RecordRef poRef = new RecordRef();
poRef.internalId = strPurchaseOrderId;
poRef.type = RecordType.purchaseOrder;
poRef.typeSpecified = true;
RecordRef[] poRefs = new RecordRef[1];
poRefs[0] = poRef;
SearchMultiSelectField poID = new SearchMultiSelectField();
poID.searchValue = poRefs;
poID.#operator = SearchMultiSelectFieldOperator.anyOf;
poID.operatorSpecified = true;
tranSearchBasic.internalId = poID;
tranSearch.basic = tranSearchBasic;
InitService();
SearchResult results = _service.search(tranSearch);
if (results.status.isSuccess && results.status.isSuccessSpecified)
{
Record[] poRecords = results.recordList;
PurchaseOrder purchaseOrder = (PurchaseOrder)poRecords[0];
PurchaseOrderItemList poItemList = purchaseOrder.itemList;
PurchaseOrderItem[] poItems = poItemList.item;
if (poItems != null && poItems.Length > 0)
{
for (var i = 0; i < poItems.Length; i++)
{
Console.WriteLine("Item Line On PO = " + poItems[i].line);
Console.WriteLine("Item Quantity = " + poItems[i].quantity);
Console.WriteLine("Item Descrition = " + poItems[i].description);
}
}
}
}
I have many albums in txt file and i want to read each line in the file while i read. I should check if the line start with Uppercase letter. So that means I should create new object of type Album, and if the line star with "0" that means is track and I should create new object of type Track and so on for an e.g of album which i want tack it from the file and store it in my java program:
Pink Floyd : Dark Side of the Moon
0:01:30 - Speak to me
0:06:48 - Brain Damage
.
.
etc.
And this is my code the file have 13 albums and each has many tracks with the period of every track.
if(Character.isUpperCase(line.charAt(0))==true) {
String[] token=line.split(":");
artistName=token[0];
albumTitle=token[1];
}
else {
tracks.add(new Track(line));
count2++;
}
album = new Album(artistName,albumTitle,tracks);
albumCollection.add(album);
so how to let the program understand that the start of album's tracks and end and then pass the array list of track to album object.
Thank
Your question is a bit difficult to understand but, I give it a try and imagined the scenario. I assume that you have already created Album and Track classes and everything works. I assume that your albums file looks as follow:
Pink Floyd : Dark Side of the Moon
0:01:30 - Speak to me
0:06:48 - Brain Damage
Another artist: Album name
0:02:33 - Whatever
0:16:21 - Blah Blah
Third artist: Album name
0:02:33 - X
0:16:21 - Y
0:02:33 - Z
0:16:21 - A
What you have to do is start reading the file line by line, which I believe that you are doing somewhere in the code that we cannot see. For each line you have the following condition which is fine
//you don't need to add == true in the if condition
if (Character.isUpperCase(line.charAt(0))) {
//Album found
} else {
//Track found
}
Every time you read a line. If Album found then you initialize an Album object and store it its artist, title and an empty list of tracks. Every time a track if found, check if the Album object is not null (if not null then it is current album) and retrieve its track list, add the new track to it and set the track list back to the Album object.
I have written the following code, assuming you have majority of it which we cannot see in this question. Go through the code and you will understand how a line is read and how an object of Album is created, how tracks are created and stored in the album. To test the following solution, copy/paste it in file same as class name and execute it, make sure you have the album.txt file containing the album.
import java.util.List;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class Track {
String track;
public Track(String track) {
this.track = track;
}
//overriding toString() method for Track class
public String toString() {
return track;
}
}
class Album {
String artistName;
String albumTitle;
List < Track > tracks = new ArrayList < Track > ();
//Album Constructor
public Album(String artistName, String albumTitle) {
this.artistName = artistName;
this.albumTitle = albumTitle;
}
public List < Track > getTracks() {
return tracks;
}
public void setTracks(List < Track > tracks) {
this.tracks = tracks;
}
//overriding the toString method for Album
public String toString() {
StringBuilder album = new StringBuilder();
album.append("Artist name: " + artistName + "\n");
album.append("\n Album title : " + albumTitle + "\n\n");
for (int i = 0; i < tracks.size(); i++) {
album.append("\n Track " + (i + 1) + ":" + tracks.get(i).toString());
}
return album.toString();
}
}
public class ReadAlbums {
public static void main(String[] args) {
List < Album > albumsCollection = new ArrayList < Album > ();
BufferedReader in = null;
try { in = new BufferedReader(new FileReader("albums.txt"));
String line;
List < Track > currentTracks = new ArrayList < Track > ();
Album album = null;
while ((line = in .readLine()) != null) {
//no need to put == true in the if condition
if (Character.isUpperCase(line.charAt(0))) {
//Album found
String[] token = line.split(":");
//If the not the first ever album then add the previous album to the collection
if (album != null) {
albumsCollection.add(album);
}
//new album object is created with artist name and album title
album = new Album(token[0], token[1]);
//new empty track list is added to the album object
album.setTracks(new ArrayList < Track > ());
} else {
//Track found
//retrieve the track from Album album
currentTracks = album.getTracks();
//Add the track to the list of tracks obtained from album
currentTracks.add(new Track(line));
//add the updated track list back to the album object
album.setTracks(currentTracks);
}
}
//add the last album in the album collections
if(album != null) {
albumsCollection.add(album);
}
//close the input stream
in.close();
} catch (IOException e) {}
System.out.println("albums : " + albums.toString());
}
}
You get the following output:
albums : [Artist name: Pink Floyd
Album title : Dark Side of the Moon
Track 1:0:01:30 - Speak to me
Track 2:0:06:48 - Brain Damage, Artist name: Another artist
Album title : This is the second album
Track 1:0:02:33 - Whatever
Track 2:0:16:21 - Blah Blah, Artist name: Third artist
Album title : This is the third album
Track 1:0:02:33 - X ]
To print the data read from file back in the same format. You need to to loop through albums and for each album retrieve the list of tracks and then print the track.
for(int i = 0; i < albumsCollection.size();i++) {
Album album = albumsCollection.get(i);
System.out.println(album.getArtistName() + ":" + album.getAlbumTitle());
List<Tracks> tracks = album.getTracks();
for(int j = 0; j < tracks.size(); j++) {
System.out.println(tracks[j].toString());
}
}
yes it like this.
The Dave Brubeck Quartet : Take Five
0:06:44 - Blue Rondo a la Turk
0:07:22 - Strange Meadow Lark
0:05:24 - Take Five
0:04:16 - Pick Up Sticks
Goldfrapp : Supernature
0:03:24 - Ooh La La
0:03:25 - Lovely 2 C U
0:04:41 - Ride a White Horse
If you really want to do it like this and if your file REALLY always has this exact structure, a quick and dirty solution would be this:
ArrayList<Track> tracks = new ArrayList<Track>();
ArrayList<Album> albumCollection = new ArrayList<Album>();
Album album;
String artistName;
String albumTitle;
String[] token;
BufferedReader br = new BufferedReader(new FileReader("albums.txt"));
try {
String line = br.readLine();
while (line != null) {
if(!Character.isDigit(line.charAt(0)) {
// there is a problem if your artist name starts with a "0" so add some more checks here
token = line.split(" : ");
artistName = token[0];
albumTitle = token[1];
if(!tracks.isEmpty()) {
album = new Album(artistName,albumTitle,tracks);
albumCollection.add(album);
tracks.clear();
}
}
else {
tracks.add(new Track(line))
}
line = br.readLine();
}
} finally {
br.close();
}
You said you want to print all tracks?!
for(Album alb : albumCollection) {
// I dont know about your implementation of the Album class but I assume:
System.out.println(alb.getTitle());
System.out.println("##TRACKS###");
ArrayList<Track> trs = alb.getTracks();
for(Track tr : trs) {
String trackName = tr.getTitle(); // I assume again..
System.out.println(trackName);
// .....
}
}