Skip to content

Commit

Permalink
Add Custom Artefact effects (#593)
Browse files Browse the repository at this point in the history
* custom effects 
authored-by: goosius1 <=>
  • Loading branch information
Goosius1 authored May 20, 2022
1 parent 4bed582 commit 028625b
Show file tree
Hide file tree
Showing 12 changed files with 441 additions and 84 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.goosius</groupId>
<artifactId>SiegeWar</artifactId>
<version>0.8.0</version>
<version>0.7.17</version>
<name>siegewar</name> <!-- Leave lower-cased -->

<properties>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/gmail/goosius/siegewar/SiegeWar.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.gmail.goosius.siegewar.listeners.SiegeWarStatusScreenListener;
import com.gmail.goosius.siegewar.listeners.SiegeWarTownEventListener;
import com.gmail.goosius.siegewar.listeners.SiegeWarTownyEventListener;
import com.gmail.goosius.siegewar.listeners.SiegeWarArtefactListener;

import java.io.File;
import java.nio.file.Files;
Expand Down Expand Up @@ -162,6 +163,7 @@ private void registerListeners() {
pm.registerEvents(new SiegeWarTownEventListener(this), this);
pm.registerEvents(new SiegeWarPlotEventListener(this), this);
pm.registerEvents(new SiegeWarStatusScreenListener(), this);
pm.registerEvents(new SiegeWarArtefactListener(this), this);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.gmail.goosius.siegewar.events;

import com.gmail.goosius.siegewar.utils.SiegeWarDominationAwardsUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ArtefactConsumeItemEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final Player consumer;
private final ItemStack artefact;
private final List<String> customEffects;

public ArtefactConsumeItemEvent(Player consumer, ItemStack artefact) {
super(!Bukkit.getServer().isPrimaryThread());
this.consumer = consumer;
this.artefact = artefact;
this.customEffects = SiegeWarDominationAwardsUtil.getCustomEffects(artefact);
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

public Player getConsumer() {
return consumer;
}

public ItemStack getArtefact() {
return artefact;
}

public List<String> getCustomEffects() {
return customEffects;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.gmail.goosius.siegewar.events;

import com.gmail.goosius.siegewar.utils.SiegeWarDominationAwardsUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ArtefactDamageEntityEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final Entity attacker;
private final Entity victim;
private final Object artefact; //Will either be Projectile or ItemStack
private final List<String> customEffects;

public ArtefactDamageEntityEvent(Entity attacker, Entity victim, Object artefact) {
super(!Bukkit.getServer().isPrimaryThread());
this.attacker = attacker;
this.victim = victim;
this.artefact = artefact;
this.customEffects = SiegeWarDominationAwardsUtil.getCustomEffects(artefact);
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

public Entity getAttacker() {
return attacker;
}

public Entity getVictim() {
return victim;
}

public Object getArtefact() {
return artefact;
}

public List<String> getCustomEffects() {
return customEffects;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.gmail.goosius.siegewar.events;

import com.gmail.goosius.siegewar.utils.SiegeWarDominationAwardsUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;

public class ArtefactThrownPotionEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final ThrownPotion artefact;
private final Collection<LivingEntity> affectedEntities;
private final List<String> customEffects;

public ArtefactThrownPotionEvent(ThrownPotion artefact, Collection<LivingEntity> affectedEntities) {
super(!Bukkit.getServer().isPrimaryThread());
this.artefact = artefact;
this.affectedEntities = affectedEntities;
this.customEffects = SiegeWarDominationAwardsUtil.getCustomEffects(artefact.getItem());
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

public ThrownPotion getArtefact() {
return artefact;
}

public List<String> getCustomEffects() {
return customEffects;
}

public Collection<LivingEntity> getAffectedEntities() {
return affectedEntities;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.gmail.goosius.siegewar.listeners;

import com.gmail.goosius.siegewar.SiegeWar;
import com.gmail.goosius.siegewar.events.ArtefactDamageEntityEvent;
import com.gmail.goosius.siegewar.settings.SiegeWarSettings;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

/**
*
* @author Goosius
*
*/
public class SiegeWarArtefactListener implements Listener {

@SuppressWarnings("unused")
private final SiegeWar plugin;

public SiegeWarArtefactListener(SiegeWar siegeWar) {
plugin = siegeWar;
}

@EventHandler (ignoreCancelled = true)
public void on(ArtefactDamageEntityEvent event) {
if (SiegeWarSettings.getWarSiegeEnabled()) {
for(String customEffect: event.getCustomEffects()) {
switch (customEffect) {
case "lightning_strike_on_hit":
event.getAttacker().getWorld().strikeLightning(event.getVictim().getLocation());
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import java.util.List;

import com.gmail.goosius.siegewar.events.ArtefactConsumeItemEvent;
import com.gmail.goosius.siegewar.events.ArtefactDamageEntityEvent;
import com.gmail.goosius.siegewar.events.ArtefactThrownPotionEvent;
import com.gmail.goosius.siegewar.utils.SiegeWarDominationAwardsUtil;
import com.gmail.goosius.siegewar.utils.SiegeWarNotificationUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand All @@ -17,13 +21,17 @@
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.potion.PotionEffectType;

import com.gmail.goosius.siegewar.Messaging;
Expand All @@ -42,6 +50,7 @@
import com.palmergames.bukkit.towny.object.Translatable;

import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.projectiles.ProjectileSource;

/**
*
Expand All @@ -60,12 +69,19 @@ public SiegeWarBukkitEventListener(SiegeWar instance) {

/*
* SW will prevent someone in a banner area from curing their poisoning with milk.
*
* Also Artefacts fire events
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerConsume(PlayerItemConsumeEvent event) {

if(SiegeWarSettings.getWarSiegeEnabled()) {
try {
//Artefact Potions
if(SiegeWarDominationAwardsUtil.isArtefact(event.getItem())) {
ArtefactConsumeItemEvent artefactEvent = new ArtefactConsumeItemEvent(event.getPlayer(), event.getItem());
Bukkit.getPluginManager().callEvent(artefactEvent);
return;
}
//Prevent milk bucket usage while attempting to gain banner control
if(event.getItem().getType() == Material.MILK_BUCKET) {
for(Siege siege: SiegeController.getSieges()) {
Expand All @@ -75,7 +91,6 @@ public void onPlayerConsume(PlayerItemConsumeEvent event) {
}
}
}

} catch (Exception e) {
SiegeWar.severe("Problem evaluating siege player consume event");
e.printStackTrace();
Expand Down Expand Up @@ -258,6 +273,31 @@ public void on(EntityDamageByEntityEvent event) {
return;
}

//Check for Artefact Usage
if(event.getDamager() instanceof Player) {
//Check if melee weapon is an artefact
ItemStack itemInMainHand = ((Player) event.getDamager()).getInventory().getItemInMainHand();
if(SiegeWarDominationAwardsUtil.isArtefact(itemInMainHand)) {
ArtefactDamageEntityEvent artefactDamageEntityEvent = new ArtefactDamageEntityEvent(event.getDamager(), event.getEntity(), itemInMainHand);
Bukkit.getPluginManager().callEvent(artefactDamageEntityEvent);
}
} else if (event.getDamager() instanceof Projectile) {
ProjectileSource shooter = ((Projectile)event.getDamager()).getShooter();
if(shooter instanceof Player) {
//Check if projectile is an artefact
if(SiegeWarDominationAwardsUtil.isArtefact(event.getDamager())) {
ArtefactDamageEntityEvent artefactDamageEntityEvent = new ArtefactDamageEntityEvent((Player)shooter, event.getEntity(), event.getDamager());
Bukkit.getPluginManager().callEvent(artefactDamageEntityEvent);
}
//Check if shooting weapon is an artefact
ItemStack itemInMainHand = ((Player)shooter).getInventory().getItemInMainHand();
if(SiegeWarDominationAwardsUtil.isArtefact(itemInMainHand)) {
ArtefactDamageEntityEvent artefactDamageEntityEvent = new ArtefactDamageEntityEvent((Player)shooter, event.getEntity(), itemInMainHand);
Bukkit.getPluginManager().callEvent(artefactDamageEntityEvent);
}
}
}

//Return if the entity being damaged is not a player
if(!(event.getEntity() instanceof Player))
return;
Expand Down Expand Up @@ -290,8 +330,7 @@ public void on (PrepareItemCraftEvent event) {
return;
if (!SiegeWarSettings.isDominationAwardsGlobalEnabled())
return;
if(event.getInventory().getResult() != null
&& event.isRepair()
if(event.isRepair()
&& SiegeWarDominationAwardsUtil.isArtefact(event.getInventory().getResult())) {
event.getInventory().setResult(null); //Cannot repair artefact
}
Expand All @@ -303,9 +342,44 @@ public void on (PrepareAnvilEvent event) {
return;
if (!SiegeWarSettings.isDominationAwardsGlobalEnabled())
return;
if(event.getResult() != null
&& SiegeWarDominationAwardsUtil.isArtefact(event.getResult())) {
if(SiegeWarDominationAwardsUtil.isArtefact(event.getResult())) {
event.setResult(null); //Cannot repair artefact
}
}

/**
* If a bow/crossbow is fired, transfer any artefact tags to the projectile
*
* @param event the event
*/
@EventHandler (ignoreCancelled = true)
public void on (EntityShootBowEvent event) {
if (!SiegeWarSettings.getWarSiegeEnabled())
return;
if (!SiegeWarSettings.isDominationAwardsGlobalEnabled())
return;
if(SiegeWarDominationAwardsUtil.isArtefact(event.getConsumable())) {
PersistentDataContainer itemStackDataContainer = event.getConsumable().getItemMeta().getPersistentDataContainer();
PersistentDataContainer projectileDataContainer = event.getProjectile().getPersistentDataContainer();
//Transfer expiry time
long expiryTime = itemStackDataContainer.get(SiegeWarDominationAwardsUtil.EXPIRATION_TIME_KEY, SiegeWarDominationAwardsUtil.EXPIRATION_TIME_KEY_TYPE);
projectileDataContainer.set(SiegeWarDominationAwardsUtil.EXPIRATION_TIME_KEY, SiegeWarDominationAwardsUtil.EXPIRATION_TIME_KEY_TYPE, expiryTime);
//Transfer custom effects
if(itemStackDataContainer.has(SiegeWarDominationAwardsUtil.CUSTOM_EFFECTS_KEY, SiegeWarDominationAwardsUtil.CUSTOM_EFFECTS_KEY_TYPE)) {
String customEffects = itemStackDataContainer.get(SiegeWarDominationAwardsUtil.CUSTOM_EFFECTS_KEY, SiegeWarDominationAwardsUtil.CUSTOM_EFFECTS_KEY_TYPE);
projectileDataContainer.set(SiegeWarDominationAwardsUtil.CUSTOM_EFFECTS_KEY, SiegeWarDominationAwardsUtil.CUSTOM_EFFECTS_KEY_TYPE, customEffects);
}
}
}

@EventHandler (ignoreCancelled = true)
public void on (PotionSplashEvent event) {
if(SiegeWarSettings.getWarSiegeEnabled()) {
//Artefact Potions
if(SiegeWarDominationAwardsUtil.isArtefact(event.getPotion().getItem())) {
ArtefactThrownPotionEvent artefactEvent = new ArtefactThrownPotionEvent(event.getPotion(), event.getAffectedEntities());
Bukkit.getPluginManager().callEvent(artefactEvent);
}
}
}
}
Loading

0 comments on commit 028625b

Please sign in to comment.