outsourced repeating code to own functions created javadoc
This commit is contained in:
30
src/de/steev/bm/changes/interaction/Death.java
Normal file
30
src/de/steev/bm/changes/interaction/Death.java
Normal file
@ -0,0 +1,30 @@
|
||||
package de.steev.bm.changes.interaction;
|
||||
|
||||
import de.steev.bm.main;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class Death {
|
||||
/**
|
||||
* handles animal loot drops only works as array
|
||||
* @param amount the drop amount
|
||||
* @param drops the dropping items
|
||||
* @param plugin plugin reference
|
||||
*/
|
||||
public void onAnimalDeath(int[] amount, Material[] drops, main plugin, EntityDeathEvent event){
|
||||
|
||||
// Random Drop amount generation DO NOT TOUCH!!!!
|
||||
int i1_amnt = ThreadLocalRandom.current().nextInt(1, amount[0] + 1);
|
||||
int i2_amnt = ThreadLocalRandom.current().nextInt(1, amount[1] + 1);
|
||||
|
||||
// Clears Vanilla drops
|
||||
event.getDrops().clear();
|
||||
|
||||
// Drops specified amount of the specified items
|
||||
for(int i = 0; i < i1_amnt; i++) { event.getEntity().getLocation().getWorld().dropItem(event.getEntity().getLocation(), new ItemStack(drops[0])); }
|
||||
for(int i = 0; i < i2_amnt; i++) { event.getEntity().getLocation().getWorld().dropItem(event.getEntity().getLocation(), new ItemStack(drops[1])); }
|
||||
}
|
||||
}
|
@ -13,6 +13,13 @@ public class Item_Recipy_Override {
|
||||
NamespacedKey item;
|
||||
static ShapedRecipe rec_item;
|
||||
|
||||
/**
|
||||
* Overrides vanilla recipes
|
||||
* @param plugin Plugin Reference
|
||||
* @param Item Item name
|
||||
* @param result the dropping item
|
||||
* @param amount the droprate
|
||||
*/
|
||||
public Item_Recipy_Override(main plugin, String Item, Material result, int amount){
|
||||
this.plugin = plugin;
|
||||
item = new NamespacedKey(plugin, Item);
|
||||
@ -22,6 +29,12 @@ public class Item_Recipy_Override {
|
||||
rec_item = new ShapedRecipe(item, itemStack);
|
||||
}
|
||||
|
||||
/**
|
||||
* registers recipe to bukkit recipe handler
|
||||
* @param ing Ingredience reference
|
||||
* @param mat material ing will reference
|
||||
* @param rec the recipe shape
|
||||
*/
|
||||
public static void register_recipy(char[] ing, Material[] mat, String[] rec){
|
||||
//Recipe shapes
|
||||
rec_item.shape(rec[0], rec[1], rec[2]);
|
||||
|
@ -5,93 +5,43 @@ import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.Ageable;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class replanting implements Listener {
|
||||
public class replanting {
|
||||
|
||||
private main plugin;
|
||||
|
||||
/**
|
||||
* event constructor
|
||||
* @param plugin plugin instance
|
||||
*/
|
||||
public replanting(main plugin) {
|
||||
plugin.getLogger().info("replanting handler Registered");
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
// TODO: change planting to a single function that gets reused as the job plugin did
|
||||
/**
|
||||
* simplified function for handling replanting works only with arrays
|
||||
* @param item the material of each drop
|
||||
* @param amount the drop amount of each item
|
||||
*/
|
||||
public static void planting(Material[] item, int[] amount, Block target){
|
||||
Ageable crop = (Ageable) target.getBlockData();
|
||||
Location loc = target.getLocation().clone().add(0.5, 0.5, 0.5);
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event){
|
||||
if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
|
||||
// TODO: validate if code may throw errors
|
||||
|
||||
// Get needed values and states
|
||||
Block target = event.getClickedBlock();
|
||||
Location loc = target.getLocation().clone().add(0.5, 0.5, 0.5);
|
||||
|
||||
System.out.println(target.getType());
|
||||
|
||||
// Detects the right block and its required action
|
||||
if(target.getType() == Material.CARROTS){
|
||||
Ageable crop = (Ageable) target.getBlockData();
|
||||
|
||||
if(crop.getAge() == crop.getMaximumAge()){
|
||||
for(int i = 0; i < 3; i++) {
|
||||
target.getLocation().getWorld().dropItem(loc, new ItemStack(Material.CARROT));
|
||||
}
|
||||
crop.setAge(0);
|
||||
target.setBlockData(crop);
|
||||
}
|
||||
} else if(target.getType() == Material.POTATOES){
|
||||
Ageable crop = (Ageable) target.getBlockData();
|
||||
|
||||
if(crop.getAge() == crop.getMaximumAge()){
|
||||
if(crop.getAge() == crop.getMaximumAge()){
|
||||
int i1_amnt = ThreadLocalRandom.current().nextInt(1, 2 + 1);
|
||||
|
||||
for(int i = 0; i < i1_amnt; i++) {
|
||||
target.getLocation().getWorld().dropItem(loc, new ItemStack(Material.POTATO));
|
||||
}
|
||||
crop.setAge(0);
|
||||
target.setBlockData(crop);
|
||||
}
|
||||
}
|
||||
} else if(target.getType() == Material.WHEAT){
|
||||
Ageable crop = (Ageable) target.getBlockData();
|
||||
|
||||
if(crop.getAge() == crop.getMaximumAge()){
|
||||
if(crop.getAge() == crop.getMaximumAge()){
|
||||
int i1_amnt = ThreadLocalRandom.current().nextInt(1, 2 + 1);
|
||||
|
||||
target.getLocation().getWorld().dropItem(loc, new ItemStack(Material.WHEAT));
|
||||
for(int i = 0; i < i1_amnt; i++) {
|
||||
target.getLocation().getWorld().dropItem(loc, new ItemStack(Material.WHEAT_SEEDS));
|
||||
}
|
||||
crop.setAge(0);
|
||||
target.setBlockData(crop);
|
||||
}
|
||||
}
|
||||
} else if(target.getType() == Material.BEETROOTS){
|
||||
Ageable crop = (Ageable) target.getBlockData();
|
||||
|
||||
if(crop.getAge() == crop.getMaximumAge()){
|
||||
if(crop.getAge() == crop.getMaximumAge()){
|
||||
int i1_amnt = ThreadLocalRandom.current().nextInt(1, 3 + 1);
|
||||
|
||||
target.getLocation().getWorld().dropItem(loc, new ItemStack(Material.BEETROOT));
|
||||
for(int i = 0; i < i1_amnt; i++) {
|
||||
target.getLocation().getWorld().dropItem(loc, new ItemStack(Material.BEETROOT_SEEDS));
|
||||
}
|
||||
crop.setAge(0);
|
||||
target.setBlockData(crop);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
if(crop.getAge() == crop.getMaximumAge()){
|
||||
for(int i = 0; i < amount[0]; i++) {
|
||||
target.getLocation().getWorld().dropItem(loc, new ItemStack(item[0]));
|
||||
}
|
||||
|
||||
for(int i = 0; i < amount[1]; i++) {
|
||||
target.getLocation().getWorld().dropItem(loc, new ItemStack(item[1]));
|
||||
}
|
||||
crop.setAge(0);
|
||||
target.setBlockData(crop);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,48 @@
|
||||
package de.steev.bm.events;
|
||||
|
||||
public class InteractEvent {
|
||||
import de.steev.bm.main;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
|
||||
public class InteractEvent implements Listener {
|
||||
|
||||
private main plugin;
|
||||
|
||||
public InteractEvent(main plugin){
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Carrot: Carrot 3
|
||||
* Potato: potato 2
|
||||
* Wheat: 2 Wheat, 2 Wheat Seeds
|
||||
* Beetroots: 1 Beetroot, 3 Beetroot seeds
|
||||
*/
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event){
|
||||
if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
|
||||
|
||||
/** the clicked block */
|
||||
Block target = event.getClickedBlock();
|
||||
/** the location of the clicked block */
|
||||
Location loc = target.getLocation().clone().add(0.5, 0.5, 0.5);
|
||||
|
||||
// Detects which type to replant
|
||||
// TODO: do the replant implementation
|
||||
switch(target.getType().toString()){
|
||||
case "CARROT":
|
||||
break;
|
||||
case "POTATO":
|
||||
break;
|
||||
case "WHEAT":
|
||||
break;
|
||||
case "BEETROOT":
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,10 @@ public class KillEvent implements Listener{
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements drops on death and can handle other deaths
|
||||
* @param event the parsed event on any death
|
||||
*/
|
||||
@EventHandler
|
||||
public void onEntityDeath(EntityDeathEvent event) {
|
||||
if(event.getEntity() instanceof Player){
|
||||
@ -29,25 +33,11 @@ public class KillEvent implements Listener{
|
||||
UUID uuid = ((Player) event.getEntity()).getPlayer().getUniqueId();
|
||||
plugin.playerDataConfig.set("" + uuid + ".death", death);
|
||||
} else if(event.getEntity() instanceof Animals) {
|
||||
|
||||
|
||||
if(event.getEntity().toString() == "CraftPig"){
|
||||
|
||||
int i1_amnt = ThreadLocalRandom.current().nextInt(1, 2 + 1);
|
||||
int i2_amnt = ThreadLocalRandom.current().nextInt(1, 2 + 1);
|
||||
|
||||
plugin.getLogger().info("Output amount: " + i1_amnt);
|
||||
plugin.getLogger().info("Output amount: " + i2_amnt);
|
||||
|
||||
event.getDrops().clear();
|
||||
|
||||
for(int i = 0; i < i1_amnt; i++) {
|
||||
event.getEntity().getLocation().getWorld().dropItem(event.getEntity().getLocation(), new ItemStack(Material.LEATHER));
|
||||
plugin.getLogger().info("Output Leather");
|
||||
}
|
||||
|
||||
for(int i = 0; i < i2_amnt; i++) {
|
||||
event.getEntity().getLocation().getWorld().dropItem(event.getEntity().getLocation(), new ItemStack(Material.PORKCHOP));
|
||||
plugin.getLogger().info("Output Porkchop");
|
||||
}
|
||||
// leather: 2, porkchop: 2
|
||||
// TODO: do the implementation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package de.steev.bm;
|
||||
|
||||
import de.steev.bm.changes.interaction.Item_Recipy_Override;
|
||||
import de.steev.bm.changes.interaction.replanting;
|
||||
import de.steev.bm.events.BedEvent;
|
||||
import de.steev.bm.events.InteractEvent;
|
||||
import de.steev.bm.events.KillEvent;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
@ -40,7 +41,7 @@ public class main extends JavaPlugin implements Listener {
|
||||
this.getLogger().info(ChatColor.AQUA + "Lade Events");
|
||||
this.getServer().getPluginManager().registerEvents(new BedEvent(this), this);
|
||||
this.getServer().getPluginManager().registerEvents(new KillEvent(this), this);
|
||||
this.getServer().getPluginManager().registerEvents(new replanting(this), this);
|
||||
this.getServer().getPluginManager().registerEvents(new InteractEvent(this), this);
|
||||
this.getServer().getPluginManager().registerEvents(this, this);
|
||||
|
||||
// PlayerData
|
||||
|
Reference in New Issue
Block a user