Skip to content

Commit

Permalink
Change how BreedListener handles eggs
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnum97 committed May 2, 2020
1 parent 728c33d commit 12c919b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/create-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
* Change Materials to match Minecraft 1.15
* Integrated code from local API
* Add workflows for issue and release creation.
* Refactored main class
* Change how plugin watches for eggs to hatch
files: build/libs/BreedablePets-${{ steps.get_version.outputs.VERSION }}.jar
gzip: folders

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/magnum/Breedable.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void onEnable () {
Common.log("Loading breed-able pets...");
cfg = configManager.getNewConfig("config.yml");
// cfg = new SimpleConfig("config.yml", plugin);
// Remain.setPlugin(plugin); //todo Add compatability
// Remain.setPlugin(plugin); // TODO Add compatability
plugin.getServer().getPluginManager().registerEvents(new MyListener(), plugin);
plugin.getServer().getPluginManager().registerEvents(new BreedListener(), plugin);
registerCommands();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void onFeed (PlayerInteractEntityEvent pie) {
Material.MELON,
Material.PUMPKIN_SEEDS,
Material.GLISTERING_MELON_SLICE).contains(hand)) {
chanceModifier = foodCalc(target, hand); // todo increase chance of egg/fertile egg by type of food.
chanceModifier = foodCalc(target, hand); // TODO increase chance of egg/fertile egg by type of food.
}
else {
return;
Expand All @@ -82,7 +82,7 @@ public void onFeed (PlayerInteractEntityEvent pie) {
sitting.setSitting(true);
player.getInventory().getItemInMainHand().setAmount(player.getInventory().getItemInMainHand().getAmount() - 1);

// Common.tell(player, "Base chance of egg: " + chanceModifier); // todo remove before deploy
// Common.tell(player, "Base chance of egg: " + chanceModifier); // TODO remove before deploy

List <Entity> nearby = target.getNearbyEntities(5, 2, 5);

Expand All @@ -98,7 +98,7 @@ public void onFeed (PlayerInteractEntityEvent pie) {
}
}

/* todo find nearest parrot
/* // TODO find nearest parrot
for (int i = 0; i < nearby.size(); i++) {
if (nearby.get(i).getType().equals(EntityType.PARROT)){
hasMate=true;
Expand All @@ -115,7 +115,7 @@ public void onFeed (PlayerInteractEntityEvent pie) {
if (hasMate) {
if (ThreadLocalRandom.current().nextInt(100) < (Breedable.getCfg().getInt("fertile-egg-chance"))) {
w.dropItemNaturally(loc, items.regEgg.clone());
// w.dropItemNaturally(loc, items.fertileEgg.clone()); // todo To be fertile egg - disabled until single throw bug fixed
// w.dropItemNaturally(loc, items.fertileEgg.clone()); // TODO To be fertile egg - disabled until single throw bug fixed
FastParticle.spawnParticle(target.getWorld(), ParticleType.HEART, target.getLocation(), 3);
FastParticle.spawnParticle(target.getWorld(), ParticleType.HEART, x, y, z, 3);
FastParticle.spawnParticle(w, ParticleType.HEART, mate.getLocation(), 3);
Expand All @@ -142,7 +142,7 @@ public void onFeed (PlayerInteractEntityEvent pie) {

}

// todo get length of configuration section and make array of item/chance pairs.
// TODO get length of configuration section and make array of item/chance pairs.
private Integer foodCalc (Entity target, Material type) {
int chance = 0;
switch (type) {
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/me/magnum/breedablepets/listeners/MyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,54 @@
import me.magnum.breedablepets.util.SpawnPets;
import org.bukkit.Material;
import org.bukkit.block.Dispenser;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

public class MyListener implements Listener {

private static HashMap <UUID, Player> flyingEggs = new HashMap <>();
private static HashMap <UUID, Player> flyingFertileEggs = new HashMap <>();
private final ItemUtil itemsAPI = new ItemUtil();

@EventHandler (priority = EventPriority.HIGH)
public void onEggThrow (PlayerEggThrowEvent e) {
UUID eggId = e.getEgg().getUniqueId();
e.setHatching(false);
if (e.getPlayer().getInventory().getItemInMainHand().isSimilar(itemsAPI.regEgg)) {
e.setHatching(false);
Common.sendBar(e.getPlayer(), "I really hope it hatches...");
if (ThreadLocalRandom.current().nextInt(100) < Breedable.getCfg().getInt("egg-change")) ;
{
SpawnPets.newParrot(e.getPlayer(), e.getEgg().getLocation());
}
flyingEggs.put(eggId, e.getPlayer());
}
if (e.getPlayer().getInventory().getItemInMainHand().isSimilar(itemsAPI.fertileEgg)) {
e.setHatching(false);
flyingFertileEggs.put(eggId, e.getPlayer());
Common.sendBar(e.getPlayer(), "You try hatching the Parrot egg...");
SpawnPets.newParrot(e.getPlayer(), e.getEgg().getLocation());

}
}

@EventHandler
public void eggHit (ProjectileHitEvent hitEvent) {
UUID projectile = hitEvent.getEntity().getUniqueId();
if (flyingEggs.containsKey(projectile)) {
// handle regular egg
if (ThreadLocalRandom.current().nextInt(100) < Breedable.getCfg().getInt("egg-change")) {
SpawnPets.newParrot(flyingEggs.get(projectile), hitEvent.getHitBlock().getLocation());
}
}
if (flyingFertileEggs.containsKey(projectile)) {
// handle fertile egg
SpawnPets.newParrot(flyingFertileEggs.get(projectile), hitEvent.getHitBlock().getLocation());
}
}

/*
@EventHandler
public void onHatch (CreatureSpawnEvent e) {
Expand All @@ -72,7 +90,7 @@ public void onHatch (CreatureSpawnEvent e) {

@EventHandler
public void onDispenseEgg (BlockDispenseEvent e) {
// todo add config option here
// TODO add config option here
if ((e.getItem() != null) && (e.getItem().getType() != Material.AIR)) {
if (e.getItem().getType() == Material.EGG) {
Dispenser dispenser = (Dispenser) e.getBlock().getState();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/me/magnum/breedablepets/util/SpawnPets.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package me.magnum.breedablepets.util;

import me.magnum.Breedable;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
Expand All @@ -45,9 +46,10 @@ public SpawnPets () {

public static void newParrot (Player player, Location location) {
World world = player.getWorld();
location = world.getHighestBlockAt(location).getLocation().add(0,1,0);
Parrot parrot = (Parrot) world.spawnEntity(location, EntityType.PARROT);
parrot.setTamed(Breedable.getCfg().getBoolean("hatches.tamed"));
parrot.setHealth(1); //todo add to config
parrot.setHealth(1); // TODO add to config
parrot.setVariant(Parrot.Variant.RED);
if (parrot.isTamed()) {
parrot.setOwner(player);
Expand Down
4 changes: 1 addition & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Config for @NAME@ version @VERSION@
version: @VERSION@
# base chance that feeding a _tamed_ parrot will produce an egg.
egg-chance: 10
# chance that feeding with a mate nearby produces an egg
Expand All @@ -13,7 +11,7 @@ rare-chance: 1
# chance modifiers increase chance of eggs depending on what food you use
# modifier of 0 does not change base chance

# This is for future version todo Add food modifiers
# This is for future version # TODO Add food modifiers
modifier:
wheat: 10
beetroot: 20
Expand Down

0 comments on commit 12c919b

Please sign in to comment.