Jump to content

Recommended Posts

Posted (edited)

Hello, recently I was cleaning up my disk and found an event from 2018 when I used to write events on my own. I'm not sure if I finished this one or if it even works, but I'm sharing the code here in case someone finds it useful as a base. The event involves two teams - the attacking blue team and the defending red team. The event lasts for 15 minutes, and the blue team must obtain a specific item mentioned in the code (which needs to be changed in the config), along with spawn locations and buffs. If the blue team obtains the item within the 15 minutes, the event ends, and the attacking team wins. If they fail to obtain the item, the defending red team wins. Cheers!

 

GiranCastleSiegeEvent.java

 

import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.item.instance.ItemInstance;
import net.sf.l2j.gameserver.model.team.PvpTeam;
import net.sf.l2j.gameserver.model.zone.ZoneId;
import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
import net.sf.l2j.gameserver.templates.spawn.SpawnTemplate;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.ArrayList;
import java.util.TimeZone;
import java.util.Collections;

public class GiranCastleSiegeEvent extends AbstractPvpEvent {

    private static final String CONFIG_FILE_PATH = "./configsiegeevent.txt";

    private Location redTeamSpawnLocation;
    private Location blueTeamSpawnLocation;
    private int eventDurationMinutes;
    private int winningItemId;
    private int[] rewardItemIds;
    private int startHour1;
    private int startMinute1;
    private int startHour2;
    private int startMinute2;
    private int startHour3;
    private int startMinute3;
    private int startHour4;
    private int startMinute4;
    private int startHour5;
    private int startMinute5;
    private int registrationNpcX;
    private int registrationNpcY;
    private int registrationNpcZ;

    private int minLevelRequirement = 76;
    private int maxLevelRequirement = 80;

    private boolean isRedTeamWinner = false;
    private boolean isBlueTeamWinner = false;

    // sprawdzanie lvl
    private List<L2PcInstance> eligiblePlayers = new ArrayList<>();

    // dead/respawn
    private List<L2PcInstance> deadPlayers = new ArrayList<>();

    public GiranCastleSiegeEvent() {
        super("GiranCastleSiege", "Giran Castle Siege Event", 15);
        loadConfig();
        spawnRegistrationNpc();
    }

    private void loadConfig() {
        try (BufferedReader br = new BufferedReader(new FileReader(CONFIG_FILE_PATH))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.trim().split("=");
                if (parts.length == 2) {
                    String key = parts[0].trim().toUpperCase();
                    String value = parts[1].trim();
                    switch (key) {
                        case "RED_TEAM_SPAWN":
                            redTeamSpawnLocation = parseLocation(value);
                            break;
                        case "BLUE_TEAM_SPAWN":
                            blueTeamSpawnLocation = parseLocation(value);
                            break;
                        case "EVENT_DURATION_MINUTES":
                            eventDurationMinutes = Integer.parseInt(value);
                            break;
                        case "WINNING_ITEM_ID":
                            winningItemId = Integer.parseInt(value);
                            break;
                        case "REWARDS_IDS":
                            String[] rewardIdsStr = value.split(",");
                            rewardItemIds = new int[rewardIdsStr.length];
                            for (int i = 0; i < rewardIdsStr.length; i++) {
                                rewardItemIds[i] = Integer.parseInt(rewardIdsStr[i].trim());
                            }
                            break;
                        case "START_HOUR_1":
                            startHour1 = Integer.parseInt(value);
                            break;
                        case "START_MINUTE_1":
                            startMinute1 = Integer.parseInt(value);
                            break;
                        case "START_HOUR_2":
                            startHour2 = Integer.parseInt(value);
                            break;
                        case "START_MINUTE_2":
                            startMinute2 = Integer.parseInt(value);
                            break;
                        case "START_HOUR_3":
                            startHour3 = Integer.parseInt(value);
                            break;
                        case "START_MINUTE_3":
                            startMinute3 = Integer.parseInt(value);
                            break;
                        case "START_HOUR_4":
                            startHour4 = Integer.parseInt(value);
                            break;
                        case "START_MINUTE_4":
                            startMinute4 = Integer.parseInt(value);
                            break;
                        case "START_HOUR_5":
                            startHour5 = Integer.parseInt(value);
                            break;
                        case "START_MINUTE_5":
                            startMinute5 = Integer.parseInt(value);
                            break;
                        case "REGISTRATION_NPC_X":
                            registrationNpcX = Integer.parseInt(value);
                            break;
                        case "REGISTRATION_NPC_Y":
                            registrationNpcY = Integer.parseInt(value);
                            break;
                        case "REGISTRATION_NPC_Z":
                            registrationNpcZ = Integer.parseInt(value);
                            break;
                        case "MIN_LEVEL_REQUIREMENT":
                            minLevelRequirement = Integer.parseInt(value);
                            break;
                        case "MAX_LEVEL_REQUIREMENT":
                            maxLevelRequirement = Integer.parseInt(value);
                            break;
                    }
                }
            }
        } catch (IOException | NumberFormatException e) {
            e.printStackTrace();
            redTeamSpawnLocation = new Location(0, 0, 0, 0);
            blueTeamSpawnLocation = new Location(0, 0, 0, 0);
            eventDurationMinutes = 15;
            winningItemId = 25543;
            rewardItemIds = new int[]{34245, 67834, 45686};
            startHour1 = 8;
            startMinute1 = 0;
            startHour2 = 12;
            startMinute2 = 0;
            startHour3 = 16;
            startMinute3 = 0;
            startHour4 = 20;
            startMinute4 = 0;
            startHour5 = 0;
            startMinute5 = 0;
            registrationNpcX = 0;
            registrationNpcY = 0;
            registrationNpcZ = 0;
            minLevelRequirement = 76;
            maxLevelRequirement = 80;
        }
    }

    private Location parseLocation(String value) {
        String[] parts = value.split(",");
        if (parts.length == 4) {
            int x = Integer.parseInt(parts[0]);
            int y = Integer.parseInt(parts[1]);
            int z = Integer.parseInt(parts[2]);
            int heading = Integer.parseInt(parts[3]);
            return new Location(x, y, z, heading);
        }
        return new Location(0, 0, 0, 0);
    }

    // spawnowanie npc
    private void spawnRegistrationNpc() {
        try {
            L2NpcTemplate template = NpcData.getInstance().getTemplate(23654);
            if (template != null) {
                SpawnTemplate spawnTemplate = new SpawnTemplate();
                spawnTemplate.setNpcId(template.getNpcId());
                spawnTemplate.setX(registrationNpcX);
                spawnTemplate.setY(registrationNpcY);
                spawnTemplate.setZ(registrationNpcZ);
                spawnTemplate.setHeading(0);

                L2Spawn spawn = new L2Spawn(spawnTemplate);
                spawn.init();
                spawn.setRespawnDelay(1);
                spawn.startRespawn();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isPlayerLevelValid(L2PcInstance player) {
        int playerLevel = player.getLevel();
        return playerLevel >= minLevelRequirement && playerLevel <= maxLevelRequirement;
    }

    // usuwanie bufow i lista buf event
    private void buffPlayers() {
        for (L2PcInstance player : eligiblePlayers) {
            player.stopAllEffects(); // Usuń wszystkie bufy
            for (int buffId : new int[]{56, 67, 35, 77, 865, 436, 356, 32, 678, 34, 12, 45, 18, 111, 22, 33, 44, 55, 66, 77, 88, 99, 98, 75}) {
                player.doCast(SkillData.getInstance().getSkill(buffId).getSkill());
            }
        }
    }

    // funcka reespawn po smierci
    private void respawnDeadPlayers() {
        for (L2PcInstance player : deadPlayers) {
            // spawn start
            PvpTeam team = getTeam(player);
            if (team != null) {
                Location spawnLocation = team == getRedTeam() ? redTeamSpawnLocation : blueTeamSpawnLocation;
                player.teleToLocation(spawnLocation, false);

                // Przywróć bufy gracza
                player.stopAllEffects();
                for (int buffId : new int[]{56, 67, 35, 77, 865, 436, 356, 32, 678, 34, 12, 45, 18, 111, 22, 33, 44, 55, 66, 77, 88, 99, 98, 75}) {
                    player.doCast(SkillData.getInstance().getSkill(buffId).getSkill());
                }
            }
        }
        deadPlayers.clear();
    }

    private class RespawnTask implements Runnable {
        private final L2PcInstance player;

        public RespawnTask(L2PcInstance player) {
            this.player = player;
        }

       
        public void run() {
            player.setIsDead(false);
            respawnDeadPlayers();
        }
    }

    
    protected Location getRedTeamSpawnLocation() {
        return redTeamSpawnLocation;
    }

    
    protected Location getBlueTeamSpawnLocation() {
        return blueTeamSpawnLocation;
    }

    
    protected void onEventStart() {
        buffPlayers(); // buff graczy
    }

    
    public void onKill(L2Character killer, L2Character victim, boolean isPet) {
        if (isRunning() && victim instanceof L2PcInstance && eligiblePlayers.contains(victim)) {
            L2PcInstance player = (L2PcInstance) victim;
            player.setIsDead(true);
            deadPlayers.add(player);
            ThreadPoolManager.getInstance().schedule(new RespawnTask(player), 10000); // Respawn po 10 sekundach
        }
    }

    
    public boolean canUseUnstuck(L2PcInstance player) {
        // usuwanie funkcji teleportu
        return !isRunning() || !eligiblePlayers.contains(player);
    }

   
    public boolean canUseTeleportScroll(L2PcInstance player, ItemInstance scroll) {
        // blokada scroli
        return !isRunning() || !eligiblePlayers.contains(player);
    }
}

 

configsiegeevent.txt

 

# Configurations for Giran Castle Siege Event
RED_TEAM_SPAWN = 10000,20000,30000,0
BLUE_TEAM_SPAWN = 40000,50000,60000,0
EVENT_DURATION_MINUTES = 15
WINNING_ITEM_ID = 25543
REWARDS_IDS = 34245,67834,45686
START_HOUR_1 = 8
START_MINUTE_1 = 0
START_HOUR_2 = 12
START_MINUTE_2 = 0
START_HOUR_3 = 16
START_MINUTE_3 = 0
START_HOUR_4 = 20
START_MINUTE_4 = 0
START_HOUR_5 = 0
START_MINUTE_5 = 0
REGISTRATION_NPC_X = 2345672364
REGISTRATION_NPC_Y = 343253535
REGISTRATION_NPC_Z = 34543545
MIN_LEVEL_REQUIREMENT = 76
MAX_LEVEL_REQUIREMENT = 80
RESPAWN_DELAY_SECONDS = 10

 

 

I immediately noticed that there is a mistake. I didn't add that players from the defending red team cannot pick up the item that causes victory for the attacking blue team... this needs to be added to the code.

 

update onKill method 

 

public void onKill(L2Character killer, L2Character victim, boolean isPet) {
    if (isRunning() && victim instanceof L2PcInstance && eligiblePlayers.contains(victim)) {
        L2PcInstance player = (L2PcInstance) victim;

        if (isRedTeamPlayer(player) && player.isCarryingItem(winningItemId)) {
            player.sendMessage("You cannot pick up the winning item as you are from the defending team.");
            return;
        }

        player.setIsDead(true);
        deadPlayers.add(player);
        ThreadPoolManager.getInstance().schedule(new RespawnTask(player), respawnDelaySeconds * 1000); 
    }
}

and update for isRedTeamPlayer  method 

 

private boolean isRedTeamPlayer(L2PcInstance player) {
    return getTeam(player) == getRedTeam();
}

Good Luck Guys 😄

Edited by L2OLDPLAYER
  • Like 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...