Skip to content

Instantly share code, notes, and snippets.

@aurorapar
Created September 2, 2019 11:15
Show Gist options
  • Save aurorapar/e5e84f5805579d7ac7a0f3dd42d12dc6 to your computer and use it in GitHub Desktop.
Save aurorapar/e5e84f5805579d7ac7a0f3dd42d12dc6 to your computer and use it in GitHub Desktop.
package hotsun;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
public class Filter implements Listener
{
public static final String durabilityTitle = ChatColor.YELLOW + "Sun Protection:" + ChatColor.WHITE;
public static final short maxDurability = 100;
ItemStack filter;
ItemMeta filterMeta;
public Filter()
{
this.filter = new ItemStack(Material.LANTERN);
this.filterMeta = this.filter.getItemMeta();
this.filterMeta.setDisplayName(ChatColor.GOLD + "Filter");
List<String> lore = new ArrayList<String>();
lore.add(ChatColor.GOLD + "Used to prevent damage from the sun");
this.filterMeta.setLore(lore);
this.filter.setItemMeta(filterMeta);
ShapedRecipe filterRecipe = new ShapedRecipe(HotSun.namespace, filter);
RecipeChoice wools = new RecipeChoice.MaterialChoice(Material.WHITE_WOOL,
Material.BLACK_WOOL, Material.GRAY_WOOL, Material.BROWN_WOOL);
filterRecipe.shape("WWW", "###", "S~S");
filterRecipe.setIngredient('#', Material.PAPER);
filterRecipe.setIngredient('W', wools);
filterRecipe.setIngredient('~', Material.SUGAR_CANE);
filterRecipe.setIngredient('S', Material.COAL_BLOCK);
Bukkit.addRecipe(filterRecipe);
}
public static boolean isFilter(ItemStack filter)
{
try
{
return filter.getItemMeta().getDisplayName().contains("Filter");
}
catch(Exception e)
{
return false;
}
}
public static short getDurability(ItemStack helmet)
{
if(!helmet.getType().toString().contains("_HELMET"))
throw new IllegalArgumentException("Only a helmet can have " + durabilityTitle);
if(!helmet.getItemMeta().hasLore())
return 0;
for(String lore : helmet.getItemMeta().getLore())
{
if(lore.contains(durabilityTitle))
{
String[] lores = lore.split(" ");
return Short.parseShort(lores[2]);
}
}
return 0;
}
public static boolean setDurability(ItemStack helmet, short durability)
{
if(durability > maxDurability)
throw new IllegalArgumentException("Durability exceeds maximum");
if(durability < 0)
throw new IllegalArgumentException("Durability must be a positive amount");
if(!helmet.getType().toString().contains("_HELMET"))
throw new IllegalArgumentException("Filter can only be applied to a helmet");
if(helmet.getItemMeta().hasLore())
{
List<String> lores = helmet.getItemMeta().getLore();
int index = -1;
for(String lore : lores)
{
if(lore.contains(Filter.durabilityTitle))
{
index=lores.indexOf(lore);
}
}
if(index!=-1)
{
lores.set(index, durabilityTitle + " " + durability + " / " + maxDurability);
}
else
{
lores.add(durabilityTitle + " " + durability + " / " + maxDurability);
}
helmet.getItemMeta().setLore(lores);
return true;
}
else
{
List<String> lores = new ArrayList<String>();
lores.add(durabilityTitle + " " + durability + " / " + maxDurability);
Bukkit.broadcastMessage(lores.get(0));
helmet.getItemMeta().setLore(lores);
return true;
}
}
}
package hotsun;
import org.bukkit.NamespacedKey;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public class HotSun extends JavaPlugin
{
Plugin instance;
static NamespacedKey namespace;
@Override
public void onEnable()
{
instance = this;
namespace = new NamespacedKey(this, this.getDescription().getName());
Timers.timer(this);
getServer().getPluginManager().registerEvents(new InventoryClicker(), this);
@SuppressWarnings("unused")
Filter filter = new Filter();
}
@Override
public void onDisable()
{
}
}
package hotsun;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class InventoryClicker implements Listener
{
@EventHandler
public void said(AsyncPlayerChatEvent event)
{
if(event.getMessage().equals("test"))
{
Player player = event.getPlayer();
if(player.getInventory().getHelmet() != null)
{
if(player.getInventory().getHelmet().getItemMeta().hasLore())
{
for(String lore: player.getInventory().getHelmet().getItemMeta().getLore())
{
player.sendMessage(lore);
}
}
else
{
player.sendMessage("No Lore");
}
}
}
}
@EventHandler
public void click(InventoryClickEvent event)
{
if(event.getCursor() != null)
{
if(Filter.isFilter(event.getCursor()))
{
if(event.getCurrentItem().getType().toString().contains("_HELMET"))
{
if(Filter.setDurability(event.getCurrentItem(), Filter.maxDurability))
{
Player player = (Player) event.getWhoClicked();
player.setItemOnCursor(null);
player.updateInventory();
player.sendMessage("You applied a filter to your helmet! (Click again if it disappeared)");
}
}
}
}
}
}
package hotsun;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
public class Timers
{
public static final short SUN_DAMAGE = 3;
public static boolean isDay(Player player)
{
// Adjusted 8 "minutes" for sunlight
// 0 = 06:00
return (player.getWorld().getTime() < 13884 || player.getWorld().getTime() > 22634);
}
public static void timer(Plugin plugin)
{
for(Player player : plugin.getServer().getOnlinePlayers())
{
if(!player.isDead())
{
Location location = player.getLocation();
location.setY(location.getY()+1);
Block block = location.getBlock();
if(block.getLightFromSky() > 10 && block.getLightLevel() > 9 && isDay(player) && player.getWorld().getEnvironment() == World.Environment.NORMAL)
{
try
{
ItemStack helmet = player.getInventory().getHelmet();
if(Filter.getDurability(helmet) > SUN_DAMAGE)
{
Filter.setDurability(helmet, (short) (Filter.getDurability(helmet) - SUN_DAMAGE));
}
else
{
Filter.setDurability(helmet, (short) 0);
player.damage(SUN_DAMAGE);
player.setFireTicks(60);
}
}
catch(Exception e)
{
e.printStackTrace();
}
player.damage(SUN_DAMAGE);
player.setFireTicks(60);
}
}
}
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
timer(plugin);
}
}, 20L);// 60 L == 3 sec, 20 ticks == 1 sec
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment