Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //IN YOUR ON ENABLE YOU MUST REGISTER THE TIMER CLASS.
- private int iTimer = 0;
- @Override
- public void onEnable() {
- System.out.println("[IceCream] - Roaming ShopKeeper Plugin by dNiym ENABLED.");
- this.iTimer = getServer().getScheduler().scheduleSyncRepeatingTask(this, new iceCreamTimer(this), 20l, 20l);
- }
- @Override
- public void onDisable() {
- iceCreamTimer.clear();
- this.getServer().getScheduler().cancelTask(this.iTimer);
- this.getServer().getScheduler().cancelTasks(this);
- }
- //TIMER CLASS
- package Timers;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import java.util.UUID;
- import org.bukkit.ChatColor;
- import org.bukkit.Effect;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.OfflinePlayer;
- import org.bukkit.Particle;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.EntityType;
- import org.bukkit.entity.Item;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemFlag;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.PlayerInventory;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.inventory.meta.SpawnEggMeta;
- import org.bukkit.material.MonsterEggs;
- import com.sk89q.worldedit.CuboidClipboard;
- import com.sk89q.worldedit.EditSession;
- import com.sk89q.worldedit.MaxChangedBlocksException;
- import com.sk89q.worldedit.Vector;
- import com.sk89q.worldedit.bukkit.BukkitWorld;
- import com.sk89q.worldguard.bukkit.WGBukkit;
- import com.sk89q.worldguard.protection.managers.RegionManager;
- import com.sk89q.worldguard.protection.regions.ProtectedRegion;
- import com.xxmicloxx.NoteBlockAPI.NBSDecoder;
- import com.xxmicloxx.NoteBlockAPI.RadioSongPlayer;
- import com.xxmicloxx.NoteBlockAPI.Song;
- import com.xxmicloxx.NoteBlockAPI.SongPlayer;
- import de.slikey.effectlib.effect.SmokeEffect;
- import de.slikey.effectlib.util.ParticleEffect;
- import iceCream.iceCreamMan;
- import io.netty.util.internal.ThreadLocalRandom;
- import items.RareItem;
- import items.mcIconMenu;
- import mobDrops.gear;
- import net.citizensnpcs.api.CitizensAPI;
- import net.citizensnpcs.api.event.DespawnReason;
- import net.citizensnpcs.api.event.NPCRightClickEvent;
- import net.citizensnpcs.api.npc.NPC;
- import net.citizensnpcs.npc.skin.SkinnableEntity;
- import net.minecraft.server.v1_12_R1.EntityTypes;
- import net.minecraft.server.v1_12_R1.TileEntity;
- public class iceCreamTimer implements Runnable {
- private iceCreamMan plugin;
- private int songCount = 0;
- private Song s = null;
- private SongPlayer sp = null;
- private SmokeEffect efx = null;
- private Long efxDelay = 0L;
- private long spawnDelay = 0L;
- private boolean spawned = false;
- private NPC npc;
- private Set<Material> validBlocks = new HashSet<Material>();
- private static World w;
- private List<Block> ceil = new ArrayList<Block>();
- // private Entity cart = null;
- public static iceCreamTimer instance;
- public static boolean forceSpawn = false;
- private Long timeout = 0L;
- private boolean named = false;
- private long nameDelay = 0L;
- private PlayerInventory inventory;
- private mcIconMenu menu;
- private Set<Player> viewers = new HashSet<Player>();
- public iceCreamTimer(iceCreamMan iceCream) {
- this.plugin = iceCream;
- this.validBlocks.add(Material.GRASS);
- this.validBlocks.add(Material.SAND);
- this.validBlocks.add(Material.WOOD);
- iceCreamTimer.w = plugin.getServer().getWorld("skyblock");
- this.ceil = getCeiling(w);
- iceCreamTimer.instance = this;
- }
- @Override
- public void run() {
- if(plugin.getServer().getOnlinePlayers().size() > 0) {
- if(this.spawned && this.named)
- {
- //do the song
- if(this.s == null)
- this.s = NBSDecoder.parse(new File("plugins/IceCream/icecreamsong.nbs"));
- if(this.efxDelay <= System.currentTimeMillis() && this.songCount < 3) {
- this.efxDelay = System.currentTimeMillis() + 500l;
- Location el = this.npc.getEntity().getLocation().clone().add(0,6,2);
- // el.getWorld().spawnParticle(Particle.NOTE, el, 10);
- Double r1 = ThreadLocalRandom.current().nextDouble(-0.5,0.5);
- Double r2 = ThreadLocalRandom.current().nextDouble(-0.5,0.5);
- el.getWorld().spawnParticle(Particle.NOTE, el, 5, r1, 0, r2);
- }
- if((this.sp == null || !this.sp.isPlaying()) && this.songCount < 3)
- {
- this.sp = new RadioSongPlayer(s);
- this.songCount ++;
- // sp.setAutoDestroy(true);
- for(Player p:this.plugin.getServer().getOnlinePlayers())
- sp.addPlayer(p);
- sp.setPlaying(true);
- }
- }
- if(iceCreamMan.isFirstRun())
- {
- iceCreamMan.setFirstRun(false);
- int minTime = plugin.getConfig().getInt("Settings.MinutesBetweenSpawnsMin");
- int maxTime = plugin.getConfig().getInt("Settings.MinutesBetweenSpawnsMax");
- int rTime = ThreadLocalRandom.current().nextInt(minTime,maxTime);
- this.spawnDelay = System.currentTimeMillis() + (rTime * 60 * 1000);
- } else if(forceSpawn) {
- forceSpawn = false;
- this.spawnDelay = 0L;
- }
- if(System.currentTimeMillis() >= this.spawnDelay && !this.spawned) {
- Location l = new Location(w, 974.5, 163, -679);
- //Location l = findSafeLoc(this.ceil);
- for(Player p:this.plugin.getServer().getOnlinePlayers())
- p.sendMessage(ChatColor.GREEN + "Oh snap! The " +ChatColor.AQUA + "Ice Cream Man " + ChatColor.GREEN + "Has arrived somewhere in spawn!");
- spawnTruck();
- for(Player p:l.getWorld().getPlayers()) {
- if(p.getLocation().distance(l) <= 6) {
- p.teleport(p.getLocation().add(0,6,0));
- p.sendMessage(ChatColor.GREEN + "Oops the Ice Cream man's truck almost ran you over!");
- }
- }
- this.setNpc(CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, UUID.fromString("3a8e7630-fa7f-4193-b030-69f380c40768"), 2, "Ice Cream Man"));
- this.getNpc().spawn(l);
- // System.out.println("Npc spawned at: " + this.cart.getLocation().toString());
- this.spawned = true;
- this.timeout = (long) (plugin.getConfig().getInt("Settings.MinutesUntilDespawn") * 60 * 1000) + System.currentTimeMillis();
- this.nameDelay = System.currentTimeMillis() + 500;
- }
- if(this.getNpc() != null && this.getNpc().isSpawned() && !this.named && System.currentTimeMillis() >= this.nameDelay)
- {
- this.getNpc().data().set(NPC.PLAYER_SKIN_USE_LATEST, false);
- this.getNpc().data().setPersistent(NPC.PLAYER_SKIN_UUID_METADATA, "hippiebeak");
- SkinnableEntity skinnable = getNpc().getEntity() instanceof SkinnableEntity ? (SkinnableEntity) getNpc().getEntity()
- : null;
- if (skinnable != null) {
- skinnable.setSkinName("HippieBeak", true);
- }
- this.named = true;
- // Minecart mcart = (Minecart) this.cart;
- // mcart.setMaxSpeed(0);
- // mcart.setGlowing(true);
- this.getNpc().setProtected(true);
- // mcart.addPassenger(this.getNpc().getEntity());
- }
- }
- if(this.spawned && this.named && System.currentTimeMillis() >= this.timeout)
- {
- /*
- if(this.getNpc() != null) {
- this.getNpc().setProtected(false);
- this.getNpc().despawn();
- this.getNpc().destroy();
- }
- if(this.cart != null)
- this.cart.remove();
- this.menu.destroy();
- this.menu = null;
- this.named = false;
- this.spawned = false;
- this.spawnDelay = System.currentTimeMillis() + (plugin.getConfig().getInt("Settings.MinutesBetweenSpawns") * 60 * 1000);
- for(Player p:this.plugin.getServer().getOnlinePlayers())
- p.sendMessage(ChatColor.GREEN + "The " +ChatColor.AQUA + "Ice Cream Man " + ChatColor.GREEN + "Has moved along! Better luck next time!");
- */
- this.remove();
- }
- }
- public static void spawnTruck()
- {
- RegionManager rm = WGBukkit.getRegionManager(w);
- //ProtectedRegion region = rm.getRegion("b_arena_area");
- ProtectedRegion region = rm.getRegion("IceCream");
- Vector minx = region.getMinimumPoint();
- boolean empty = true;
- if(empty == true)
- {
- File file = new File("plugins/WorldEdit/schematics/icecream.schematic");
- World world = w;
- int aX = (int) minx.getX();
- int aY = (int) minx.getY();
- int aZ = (int) minx.getZ();
- BukkitWorld BW = new BukkitWorld(world);
- @SuppressWarnings("deprecation")
- EditSession es = new EditSession(BW, 2000000);
- // Alternatives: ( class com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard,class com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat )
- CuboidClipboard c =null;
- try {
- c = CuboidClipboard.loadSchematic(file);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- System.out.println("[IceCream] Could not load the IceCream.schematic!! " );
- e.printStackTrace();
- }
- if (c == null)
- return;
- try {
- c.place(es, minx, false);
- } catch (MaxChangedBlocksException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } //.paste() or .place()?
- }
- }
- public static void clear()
- {
- if(instance != null)
- instance.remove();
- }
- private void remove() {
- if(this.menu != null) {
- this.menu.destroy();
- }
- this.menu = null;
- this.named = false;
- this.songCount = 0;
- this.s = null;
- if(this.sp != null)
- this.sp.destroy();
- this.sp= null;
- ProtectedRegion r = iceCreamMan.WGP.getRegionManager(w).getRegion("icecream");
- int minX = r.getMinimumPoint().getBlockX();
- int minY = r.getMinimumPoint().getBlockY();
- int minZ = r.getMinimumPoint().getBlockZ();
- int maxX = r.getMaximumPoint().getBlockX();
- int maxY = r.getMaximumPoint().getBlockY();
- int maxZ = r.getMaximumPoint().getBlockZ();
- int count = 0;
- for(int x = minX; x < maxX; x++)
- for(int y = minY; y < maxY; y++)
- for(int z = minZ; z < maxZ; z++) {
- Block b = w.getBlockAt(x, y, z);
- b.setType(Material.AIR);
- count++;
- }
- if(this.getNpc() != null) {
- for(Entity e:this.getNpc().getEntity().getNearbyEntities(10, 10, 10))
- if(e instanceof Item)
- e.remove();
- this.getNpc().setProtected(false);
- this.getNpc().despawn(DespawnReason.REMOVAL);
- //this.getNpc().destroy();
- }
- NPC npc = CitizensAPI.getNPCRegistry().getById(2);
- if(npc != null) {
- npc.despawn(DespawnReason.REMOVAL);
- if(npc != null)
- npc.destroy();
- }
- System.out.println("[IceCream] Removed " + count + " Blocks. From Y: " + minY + " to " + maxY);
- this.spawned = false;
- forceSpawn = false;
- int minTime = plugin.getConfig().getInt("Settings.MinutesBetweenSpawnsMin");
- int maxTime = plugin.getConfig().getInt("Settings.MinutesBetweenSpawnsMax");
- int rTime = ThreadLocalRandom.current().nextInt(minTime,maxTime);
- this.spawnDelay = System.currentTimeMillis() + (rTime * 60 * 1000);
- for(Player p:this.plugin.getServer().getOnlinePlayers())
- p.sendMessage(ChatColor.GREEN + "The " +ChatColor.AQUA + "Ice Cream Man " + ChatColor.GREEN + "Has moved along! Better luck next time!");
- }
- private Location findSafeLoc(List<Block> Ceiling) {
- int maxTries = 200;
- Block safe = null;
- for(int i = 0; i < maxTries; i ++)
- {
- int pos = ThreadLocalRandom.current().nextInt(0, this.ceil.size());
- Block b = this.ceil.get(pos);
- boolean found = false;
- int x = b.getX();
- int maxY = b.getY();
- int z = b.getZ();
- for(int y = maxY; y > 0; y--) {
- for(Material mat:this.validBlocks) {
- if(iceCreamTimer.w.getBlockTypeIdAt(x,y,z) == mat.getId())
- {
- return iceCreamTimer.w.getBlockAt(x, y+2, z).getLocation();
- }
- }
- }
- }
- return null;
- }
- private List<Block> getCeiling(World w) {
- List<Block> ceiling = new ArrayList<Block>();
- RegionManager rm = iceCreamMan.WGP.getRegionManager(w);
- ProtectedRegion rg = rm.getRegion("IceCream");
- int minX = rg.getMinimumPoint().getBlockX();
- int minY = rg.getMinimumPoint().getBlockY();
- int minZ = rg.getMinimumPoint().getBlockZ();
- int maxX = rg.getMaximumPoint().getBlockX();
- int maxY = rg.getMaximumPoint().getBlockY();
- int maxZ = rg.getMaximumPoint().getBlockZ();
- for(int x = minX; x < maxX; x++)
- for(int z = minZ;z < maxZ;z++)
- ceiling.add(w.getBlockAt(x, maxY, z));
- return ceiling;
- }
- public boolean handleClick(NPCRightClickEvent e) {
- if(this.getNpc() == null || this.getNpc() != e.getNPC()) {
- return false;
- }
- if(this.getNpc().getName().equalsIgnoreCase("Ice Cream Man"))
- {
- if(this.menu == null) //first time its been opened
- {
- this.menu = new mcIconMenu("Ice Cream Man", 9, new mcIconMenu.OptionClickEventHandler() {
- @Override
- public void onOptionClick(mcIconMenu.OptionClickEvent event) {
- ItemStack buyItem = iceCreamTimer.instance.menu.getIcon(event.getPosition());
- if(buyItem == null)
- return;
- event.setWillClose(false);
- //do code here to buy the item.
- Player p = event.getPlayer();
- int price = 0;
- ItemMeta im = buyItem.getItemMeta();
- List<String> lore = im.getLore();
- for(String s:lore) {
- if(s.startsWith(ChatColor.AQUA + "Cost:"))
- {
- String[] cost = s.split(":");
- price = Integer.parseInt(cost[1].trim());
- }
- }
- OfflinePlayer op = plugin.getServer().getOfflinePlayer(p.getUniqueId());
- if(iceCreamMan.econ.getBalance(op) <= price)
- {
- p.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "(Ice Cream Man) " + ChatColor.GRAY + "You don't have " + ChatColor.RED+ "$" + ChatColor.RED + price + ChatColor.GRAY + " to buy " + ChatColor.RED + buyItem.getType().name());
- event.setWillClose(true);
- } else {
- //can afford
- p.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "(Ice Cream Man) " + ChatColor.GRAY + "Sold you " + ChatColor.GREEN+ "(1) " + ChatColor.RED + buyItem.getType().name() + ChatColor.GRAY + " for " + ChatColor.RED + "$"+ ChatColor.RED + price);
- iceCreamMan.econ.withdrawPlayer(op, price);
- //ItemStack toPlr = new ItemStack(buyItem.getType(),1);
- ItemStack toPlr = buyItem.clone();
- toPlr.setAmount(1);
- if(buyItem.getType() == Material.MONSTER_EGG)
- {
- org.bukkit.inventory.meta.SpawnEggMeta meta = (SpawnEggMeta) toPlr.getItemMeta();
- meta.setSpawnedType(EntityType.fromId(toPlr.getDurability()));
- meta.setDisplayName(EntityType.fromId(toPlr.getDurability()).getName());
- toPlr.setItemMeta(meta);
- }
- p.getInventory().addItem(toPlr);
- int newAmt = buyItem.getAmount() -1;
- if(newAmt <= 0)
- iceCreamTimer.instance.menu.removeOption(event.getPosition());
- else
- iceCreamTimer.instance.menu.updateQty(event.getPosition(), newAmt);
- event.getPlayer().updateInventory();
- boolean empty = true;
- for(int i = 0; i < 3;i++)
- {
- ItemStack is = iceCreamTimer.instance.menu.getIcon(i);
- if(is != null && is.getType() != Material.AIR)
- empty = false;
- }
- Inventory inv = iceCreamTimer.instance.menu.open(event.getPlayer());
- iceCreamTimer.instance.viewers.add(event.getPlayer());
- //test
- if(inv != null && inv.getSize() > 0)
- for(Player plr:iceCreamTimer.instance.viewers)
- {
- if(empty) {
- plr.closeInventory();
- plr.sendMessage("The " + ChatColor.AQUA + "Ice Cream Man" + ChatColor.RESET + " is all sold out!");
- iceCreamTimer.clear();
- } else {
- if(plr.getName().equalsIgnoreCase(event.getPlayer().getName()))
- continue;
- iceCreamTimer.instance.menu.open(plr);
- }
- }
- event.setWillClose(false);
- }
- }
- }, plugin);
- for(int i = 0; i < 3; i++) {
- RareItem ri = getRandomItem();
- if(ri == null)
- continue;
- for(int z = 0;z < 3; z++)
- if(this.menu.getIcon(z) != null)
- if(ri.getMat().name().equalsIgnoreCase(this.menu.getIcon(z).getType().name()))
- continue;
- int maxAmt = 1;
- if(ri.getMaxAmt() > 1)
- maxAmt = ThreadLocalRandom.current().nextInt(1,ri.getMaxAmt());
- ItemStack is3 = new ItemStack(ri.getMat(), maxAmt);
- if(ri.getData() != 0)
- is3.setDurability(ri.getData());
- ItemMeta im3 = is3.getItemMeta();
- im3.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
- int costMod = 0;
- if(iceCreamMan.getEnchantables().contains(is3.getType()))
- {
- System.out.println("Found an item to enchant!");
- gear.randoEnchants(is3);
- for(int z = 0; z < 5; z++)
- {
- if(ThreadLocalRandom.current().nextInt(0,100) >= 50)
- gear.randoEnchants(is3);
- }
- im3.removeItemFlags(ItemFlag.HIDE_ENCHANTS);
- im3.removeItemFlags(ItemFlag.HIDE_ATTRIBUTES);
- costMod = is3.getEnchantments().size();
- }
- //enchantables?
- is3.setItemMeta(im3);
- String[] lore = new String[9];
- String end = "";
- if(costMod > 0)
- end = ChatColor.AQUA + "Cost: " + ri.getMinPrice() * costMod;
- else
- end = ChatColor.AQUA + "Cost: " + ThreadLocalRandom.current().nextInt(ri.getMinPrice(),ri.getMaxPrice());
- lore = mcIconMenu.formatLore(ri.getDesc(), 30, ChatColor.AQUA, end);
- //String[] lore = mcIconMenu.formatLore("Test", 30, ChatColor.AQUA);
- String name = is3.getType().name();
- if(is3.getType() == Material.MONSTER_EGG)
- name = "Spawn " + EntityType.fromId(is3.getDurability());
- this.menu.setOption(i, is3, name, lore);
- }
- }
- }
- iceCreamTimer.instance.viewers.add(e.getClicker());
- if(this.menu != null)
- this.menu.open(e.getClicker());
- else {
- e.getClicker().sendMessage(ChatColor.AQUA + "Oh no! It appears the Ice Cream Man is all sold out!");
- iceCreamTimer.instance.viewers.remove(e.getClicker());
- }
- return true;
- /*
- if(items.length == 0)
- items = new ItemStack[3];
- System.out.println("Made Inventory");
- if(items == null || items.length == 0)
- System.out.println("Empty item list :");
- else
- this.inventory.setContents(items);
- this.inventory.openInventory(e.getClicker());
- System.out.println("Opened for: " + e.getClicker().getName() + " size: " + this.inventory.getContents().length );
- }
- }
- */
- }
- private RareItem getRandomItem() {
- if(RareItem.loadedItems.size() == 1) {
- RareItem ri = RareItem.loadedItems.get(0);
- return ri;
- }
- for(int i = 0; i < RareItem.loadedItems.size(); i ++)
- {
- int selection = ThreadLocalRandom.current().nextInt(0,RareItem.loadedItems.size());
- RareItem ri = RareItem.loadedItems.get(selection);
- if(ThreadLocalRandom.current().nextDouble(0, 100) <= ri.getChance() )
- return ri;
- }
- return null;
- }
- private ItemStack getRandomItem2() {
- if(RareItem.loadedItems.size() == 1) {
- RareItem ri = RareItem.loadedItems.get(0);
- int maxAmt = 1;
- if(ri.getMaxAmt() > 1)
- maxAmt = ThreadLocalRandom.current().nextInt(1,ri.getMaxAmt());
- return new ItemStack(ri.getMat(),maxAmt);
- }
- for(int i = 0; i < RareItem.loadedItems.size(); i ++)
- {
- int selection = ThreadLocalRandom.current().nextInt(0,RareItem.loadedItems.size());
- RareItem ri = RareItem.loadedItems.get(selection);
- if(ThreadLocalRandom.current().nextDouble(0, 100) <= ri.getChance() )
- {
- int maxAmt = 1;
- if(ri.getMaxAmt() > 1)
- maxAmt = ThreadLocalRandom.current().nextInt(1,ri.getMaxAmt());
- return new ItemStack(ri.getMat(),maxAmt);
- }
- }
- return null;
- }
- public NPC getNpc() {
- return npc;
- }
- public void setNpc(NPC npc) {
- this.npc = npc;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement