Skip to content

Commit

Permalink
Discord webhook (#875)
Browse files Browse the repository at this point in the history
* Added discord webhook
* By Ewof
  • Loading branch information
ewof authored Jul 25, 2023
1 parent 5ef8ae7 commit b41b93f
Show file tree
Hide file tree
Showing 18 changed files with 751 additions and 38 deletions.
50 changes: 28 additions & 22 deletions src/main/java/com/gmail/goosius/siegewar/SiegeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.gmail.goosius.siegewar.enums.SiegeType;
import com.gmail.goosius.siegewar.events.PreSiegeCampEvent;
import com.gmail.goosius.siegewar.events.SiegeRemoveEvent;
import com.gmail.goosius.siegewar.events.SiegeCampStartEvent;
import com.gmail.goosius.siegewar.events.SiegeWarStartEvent;
import com.gmail.goosius.siegewar.settings.SiegeWarSettings;
import com.gmail.goosius.siegewar.utils.DataCleanupUtil;
Expand Down Expand Up @@ -481,7 +482,9 @@ public static void startSiege(Block bannerBlock,
SiegeController.setSiege(targetTown, true);
SiegeController.putTownInSiegeMap(targetTown, siege);

sendGlobalSiegeStartMessage(siege);

Translatable startMessage = getGlobalSiegeStartMessage(siege);
Messaging.sendGlobalMessage(startMessage);

SiegeWarMoneyUtil.payUpfrontSiegeStartCost(siege);

Expand Down Expand Up @@ -510,36 +513,35 @@ public static void startSiege(Block bannerBlock,
SiegeController.saveSiege(siege);

//Call event
Bukkit.getPluginManager().callEvent(new SiegeWarStartEvent(siege, townOfSiegeStarter));
Bukkit.getPluginManager().callEvent(new SiegeWarStartEvent(siege, townOfSiegeStarter, startMessage.defaultLocale()));
}

private static void sendGlobalSiegeStartMessage(Siege siege) {
public static Translatable getGlobalSiegeStartMessage(Siege siege) {
switch (siege.getSiegeType()) {

case REVOLT:
return
Translatable.of("msg_revolt_siege_started",
siege.getTown().getName(),
siege.getAttacker().getName()
);
default:
case CONQUEST:
if (siege.getTown().hasNation()) {
Messaging.sendGlobalMessage(
return
Translatable.of("msg_conquest_siege_started_nation_town",
siege.getAttacker().getName(),
TownyAPI.getInstance().getTownNationOrNull(siege.getTown()).getName(),
siege.getTown().getName()
));
siege.getAttacker().getName(),
TownyAPI.getInstance().getTownNationOrNull(siege.getTown()).getName(),
siege.getTown().getName()
);
} else {
Messaging.sendGlobalMessage(
return
Translatable.of("msg_conquest_siege_started_neutral_town",
siege.getAttacker().getName(),
siege.getTown().getName()
));
siege.getAttacker().getName(),
siege.getTown().getName()
);
}
break;

case REVOLT:
Messaging.sendGlobalMessage(
Translatable.of("msg_revolt_siege_started",
siege.getTown().getName(),
siege.getAttacker().getName()
));
break;

}
}

Expand Down Expand Up @@ -631,10 +633,14 @@ public static void beginSiegeCamp(SiegeCamp camp) throws TownyException {
if (SiegeCampUtil.hasFailedCamp(camp.getTargetTown(), camp.getTownOfSiegeStarter()))
throw new TownyException(Translatable.of("msg_err_too_soon_since_your_last_siegecamp"));

Translatable translatable = Translatable.of("attacker_has_begun_a_siegecamp_session", camp.getTownOfSiegeStarter(), camp.getTargetTown(), SiegeWarSettings.getSiegeCampPointsForSuccess(), SiegeWarSettings.getSiegeCampDurationInMinutes());

// Broadcast a message
Messaging.sendGlobalMessage(Translatable.of("attacker_has_begun_a_siegecamp_session", camp.getTownOfSiegeStarter(), camp.getTargetTown(), SiegeWarSettings.getSiegeCampPointsForSuccess(), SiegeWarSettings.getSiegeCampDurationInMinutes()));
Messaging.sendGlobalMessage(translatable);
// Add to SiegeCamp list and begin Evaluating this SiegeCamp for success.
addSiegeCamp(camp);
SiegeWar.getSiegeWar().getScheduler().run(camp.getBannerBlock().getLocation(), ()-> SiegeCampUtil.evaluateCamp(camp, true));
// Call event
Bukkit.getPluginManager().callEvent(new SiegeCampStartEvent(camp, translatable.defaultLocale()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ private void parseSiegeWarSiegeCommand(CommandSender sender, String[] args) {
case "remove":
//Remove siege from system
SiegeController.removeSiege(siege);
Messaging.sendGlobalMessage(Translatable.of("msg_swa_remove_siege"));
Messaging.sendMsg(sender, Translatable.of("msg_swa_remove_siege_success"));
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gmail.goosius.siegewar.events;

import com.palmergames.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -8,9 +9,11 @@
public class BattleSessionEndedEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final String message;

public BattleSessionEndedEvent() {
public BattleSessionEndedEvent(String message) {
super(!Bukkit.getServer().isPrimaryThread());
this.message = message;
}

@NotNull
Expand All @@ -22,4 +25,8 @@ public HandlerList getHandlers() {
public static HandlerList getHandlerList() {
return handlers;
}

public String getMessage() {
return LegacyComponentSerializer.legacySection().deserialize(message).content();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gmail.goosius.siegewar.events;

import com.palmergames.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -8,9 +9,11 @@
public class BattleSessionStartedEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final String message;

public BattleSessionStartedEvent() {
public BattleSessionStartedEvent(String message) {
super(!Bukkit.getServer().isPrimaryThread());
this.message = message;
}

@NotNull
Expand All @@ -23,4 +26,7 @@ public static HandlerList getHandlerList() {
return handlers;
}

public String getMessage() {
return LegacyComponentSerializer.legacySection().deserialize(message).content();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.gmail.goosius.siegewar.events;
import com.gmail.goosius.siegewar.enums.SiegeType;
import com.gmail.goosius.siegewar.objects.SiegeCamp;
import com.gmail.goosius.siegewar.settings.SiegeWarSettings;
import com.palmergames.adventure.text.serializer.legacy.LegacyComponentSerializer;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownBlock;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

/**
* An event that marks the start of a {@link SiegeCamp}.
*
* SiegeCamps precede Sieges, acting as a minigame of sorts, in which the
* Siege-starter must gain an amount of points:
* {@link SiegeWarSettings#getSiegeCampPointsForSuccess()}, in order for the
* Siege to begin in earnest. This gives towns an opportunity to disrupt two
* friendly towns from acting out a mock-siege, in order to get around being
* sieged by actual enemies.
*
* @author ewof
* @since 2.8.0
*/
public class SiegeCampStartEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private final SiegeCamp siegeCamp;
private final String message;

public SiegeCampStartEvent(SiegeCamp camp, String message) {
super(!Bukkit.getServer().isPrimaryThread());
this.siegeCamp = camp;
this.message = message;
}

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

public static HandlerList getHandlerList() {
return handlers;
}

public Town getTownOfSiegeStarter() {
return siegeCamp.getTownOfSiegeStarter();
}

public Nation getNation() {
return siegeCamp.getSiegeType().equals(SiegeType.REVOLT) ? (Nation) siegeCamp.getDefender() : (Nation) siegeCamp.getAttacker();
}

public Block getFlag() {
return siegeCamp.getBannerBlock();
}

public TownBlock getTownBlock() {
return siegeCamp.getTownBlock();
}

public Town getTargetTown() {
return siegeCamp.getTargetTown();
}

public SiegeType getSiegeType() {
return siegeCamp.getSiegeType();
}

public String getMessage() {
return LegacyComponentSerializer.legacySection().deserialize(message).content();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.gmail.goosius.siegewar.events;

import com.gmail.goosius.siegewar.objects.Siege;
import com.palmergames.bukkit.towny.object.Nation;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* This event is triggered immediately after a siege ends
Expand All @@ -27,6 +29,7 @@ public class SiegeEndEvent extends Event {
private final String attackerName;
private final String defenderName;
private final String besiegedTownName;
private final Nation nation;

public SiegeEndEvent(Siege siege) {
super(!Bukkit.getServer().isPrimaryThread());
Expand All @@ -36,6 +39,7 @@ public SiegeEndEvent(Siege siege) {
this.attackerName = siege.getAttackerName();
this.defenderName = siege.getDefenderName();
this.besiegedTownName = siege.getTown().getName();
this.nation = siege.isRevoltSiege() ? siege.getTown().getNationOrNull() : (Nation)siege.getAttackingNationIfPossibleElseTown();
}

@NotNull
Expand Down Expand Up @@ -89,4 +93,8 @@ public String getBesiegedTownName() {
public String getDefenderName() {
return defenderName;
}

public Nation getNation() {
return nation;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.gmail.goosius.siegewar.events;

import com.gmail.goosius.siegewar.objects.Siege;
import com.palmergames.adventure.text.serializer.legacy.LegacyComponentSerializer;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Translatable;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -24,6 +27,7 @@ public class SiegeRemoveEvent extends Event {
private final String attackerName;
private final String defenderName;
private final String besiegedTownName;
private final Nation nation;

public SiegeRemoveEvent(Siege siege) {
super(!Bukkit.getServer().isPrimaryThread());
Expand All @@ -33,6 +37,7 @@ public SiegeRemoveEvent(Siege siege) {
this.attackerName = siege.getAttackerName();
this.defenderName = siege.getDefenderName();
this.besiegedTownName = siege.getTown().getName();
this.nation = siege.isRevoltSiege() ? siege.getTown().getNationOrNull() : (Nation)siege.getAttackingNationIfPossibleElseTown();
}

@NotNull
Expand Down Expand Up @@ -86,4 +91,12 @@ public String getBesiegedTownName() {
public String getDefenderName() {
return defenderName;
}

public Nation getNation() {
return nation;
}

public String getMessage() {
return LegacyComponentSerializer.legacySection().deserialize(Translatable.of("msg_swa_remove_siege", getBesiegedTownName()).defaultLocale()).content();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gmail.goosius.siegewar.events;

import com.gmail.goosius.siegewar.objects.Siege;
import com.palmergames.adventure.text.serializer.legacy.LegacyComponentSerializer;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Town;
import org.bukkit.block.Block;
Expand All @@ -17,14 +18,16 @@ public class SiegeWarStartEvent extends Event {
private final Nation nation;
private final Block flag;
private final Town targetTown;
private final String message;

public SiegeWarStartEvent(Siege siege, Town townOfSiegeStarter) {
public SiegeWarStartEvent(Siege siege, Town townOfSiegeStarter, String message) {
this.siege = siege;
this.siegeType = siege.getSiegeType().getName();
this.targetTown = siege.getTown();
this.townOfSiegeStarter = townOfSiegeStarter;
this.nation = siege.isRevoltSiege() ? targetTown.getNationOrNull() : (Nation)siege.getAttackingNationIfPossibleElseTown();
this.flag = siege.getFlagLocation().getBlock();
this.message = message;
}

@NotNull
Expand Down Expand Up @@ -60,4 +63,8 @@ public Nation getNation() {
public Town getTargetTown() {
return targetTown;
}

public String getMessage() {
return LegacyComponentSerializer.legacySection().deserialize(message).content();
}
}
Loading

0 comments on commit b41b93f

Please sign in to comment.