Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
refactor: applying minor caching of variables in a few files. (#599)
Browse files Browse the repository at this point in the history
* refactor: applying minor caching of variables in a few files.

* refactor: fixing spacing issues and changing generic data names
  • Loading branch information
TheAvonian authored and DevScyu committed Oct 11, 2022
1 parent 47e9a95 commit e1154a0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 49 deletions.
85 changes: 50 additions & 35 deletions src/main/java/com/wynntils/modules/core/events/ServerEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public class ServerEvents implements Listener {

/**
* Does 5 different things and is triggered when the user joins Wynncraft:
* - Register the pipeline that intercepts INCOMING Packets
* @see PacketIncomingFilter
* - Register the pipline that intercepts OUTGOING Packets
* @see PacketOutgoingFilter
* - Check if the mod has an update available
* - Updates the overlayPlayerList to the Wynntils Version
* - Register the pipeline that intercepts INCOMING Packets
*
* @param e Represents the event
* @see PacketIncomingFilter
* - Register the pipline that intercepts OUTGOING Packets
* @see PacketOutgoingFilter
* - Check if the mod has an update available
* - Updates the overlayPlayerList to the Wynntils Version
*/
@SubscribeEvent
public void joinServer(FMLNetworkEvent.ClientConnectedToServerEvent e) {
Expand All @@ -89,24 +89,24 @@ public void joinServer(FMLNetworkEvent.ClientConnectedToServerEvent e) {

/**
* Called when the user joins a Wynncraft World, used to register some stuff:
* - Make the player use the command /friend list in order to gatter the user friend list
* - Check if the last user class was registered if not, make the player execute /class to register it
* - Updates the last class
* - Updates and check the Download Queue
* - Make the player use the command /friend list in order to gatter the user friend list
* - Check if the last user class was registered if not, make the player execute /class to register it
* - Updates the last class
* - Updates and check the Download Queue
*
* @param e Represents the event
*/
@SubscribeEvent(priority = EventPriority.LOWEST)
public void joinWorldEvent(WynnWorldEvent.Join e) {
if (PlayerInfo.get(CharacterData.class).getClassId() == -1 || CoreDBConfig.INSTANCE.lastClass == ClassType.NONE)
CharacterData characterData = PlayerInfo.get(CharacterData.class);
if ((characterData != null && characterData.getClassId() == -1) || CoreDBConfig.INSTANCE.lastClass == ClassType.NONE)
McIf.player().sendChatMessage("/class");

// This codeblock will only be executed if the Wynncraft AUTOJOIN setting is enabled
// Reason: When you join a world with autojoin enabled, your current class is NONE,
// while joining without autojoin will make your current class into the selected over the character selection.
CharacterData data = PlayerInfo.get(CharacterData.class);
if (data.getCurrentClass() == ClassType.NONE && CoreDBConfig.INSTANCE.lastClass != ClassType.NONE) {
data.updatePlayerClass(CoreDBConfig.INSTANCE.lastClass, CoreDBConfig.INSTANCE.lastClassIsReskinned);
if (characterData != null && characterData.getCurrentClass() == ClassType.NONE && CoreDBConfig.INSTANCE.lastClass != ClassType.NONE) {
characterData.updatePlayerClass(CoreDBConfig.INSTANCE.lastClass, CoreDBConfig.INSTANCE.lastClassIsReskinned);
}

if (Reference.onWars) return; // avoid dispatching commands while in wars/nether
Expand All @@ -130,7 +130,7 @@ public void joinWorldEvent(WynnWorldEvent.Join e) {
/**
* Detects and register the current friend list of the user
* Called when the client receives a chat message
*
* <p>
* Also detects the guild list messages to register the guild members
*
* @param e Represents the Event
Expand All @@ -142,15 +142,20 @@ public void chatMessage(ClientChatReceivedEvent e) {
}
PartyManager.handleMessages(e.getMessage()); // party messages here

SocialData socialData = PlayerInfo.get(SocialData.class);
if (socialData == null) {
return;
}

String messageText = McIf.getUnformattedText(e.getMessage());
String formatted = McIf.getFormattedText(e.getMessage());
Matcher m = FRIENDS_LIST.matcher(formatted);
if (m.find() && m.group(1).equals(McIf.player().getName())) {
String[] friends = m.group(2).split(", ");

Set<String> friendsList = PlayerInfo.get(SocialData.class).getFriendList();
Set<String> friendsList = socialData.getFriendList();
Set<String> newFriendsList = new HashSet<>(Arrays.asList(friends));
PlayerInfo.get(SocialData.class).setFriendList(newFriendsList);
socialData.setFriendList(newFriendsList);

FrameworkManager.getEventBus().post(new WynnSocialEvent.FriendList.Remove(Sets.difference(friendsList, newFriendsList), false));
FrameworkManager.getEventBus().post(new WynnSocialEvent.FriendList.Add(Sets.difference(newFriendsList, friendsList), false));
Expand All @@ -176,7 +181,7 @@ public void chatMessage(ClientChatReceivedEvent e) {
if (waitingForGuildList) e.setCanceled(true);

String[] splitMessage = messageText.split(" ");
if (PlayerInfo.get(SocialData.class).addGuildMember(splitMessage[1])) {
if (socialData.addGuildMember(splitMessage[1])) {
FrameworkManager.getEventBus().post(new WynnSocialEvent.Guild.Join(splitMessage[1]));
}
return;
Expand All @@ -186,14 +191,14 @@ public void chatMessage(ClientChatReceivedEvent e) {
if (!splittedText[1].equalsIgnoreCase("has")) return;

if (splittedText[2].equalsIgnoreCase("joined")) {
if (PlayerInfo.get(SocialData.class).addGuildMember(splittedText[0])) {
if (socialData.addGuildMember(splittedText[0])) {
FrameworkManager.getEventBus().post(new WynnSocialEvent.Guild.Join(splittedText[0]));
}
return;
}

if (splittedText[2].equalsIgnoreCase("kicked")) {
if (PlayerInfo.get(SocialData.class).removeGuildMember(splittedText[3])) {
if (socialData.removeGuildMember(splittedText[3])) {
FrameworkManager.getEventBus().post(new WynnSocialEvent.Guild.Leave(splittedText[3]));
}
}
Expand All @@ -203,21 +208,26 @@ public void chatMessage(ClientChatReceivedEvent e) {
/**
* Detects if the user added or removed a user from their friend list
* Called when the user execute /friend add or /friend remove
*
* <p>
* Also detects the guild list command used to parse the entire guild list
*
* @param e Represents the Event
*/
@SubscribeEvent
public void addFriend(ClientChatEvent e) {
SocialData socialData = PlayerInfo.get(SocialData.class);
if (socialData == null) {
return;
}

if (e.getMessage().startsWith("/friend add ")) {
String addedFriend = e.getMessage().replace("/friend add ", "");
if (PlayerInfo.get(SocialData.class).addFriend(addedFriend)) {
if (socialData.addFriend(addedFriend)) {
FrameworkManager.getEventBus().post(new WynnSocialEvent.FriendList.Add(Collections.singleton(addedFriend), true));
}
} else if (e.getMessage().startsWith("/friend remove ")) {
String removedFriend = e.getMessage().replace("/friend remove ", "");
if (PlayerInfo.get(SocialData.class).removeFriend(removedFriend)) {
if (socialData.removeFriend(removedFriend)) {
FrameworkManager.getEventBus().post(new WynnSocialEvent.FriendList.Remove(Collections.singleton(removedFriend), true));
}
} else if (e.getMessage().startsWith("/guild list") || e.getMessage().startsWith("/gu list")) {
Expand Down Expand Up @@ -257,7 +267,8 @@ public void onJoinServer(WynncraftServerEvent.Login e) {
*/
@SubscribeEvent
public void onJoinLobby(WynnClassChangeEvent e) {
if (!Reference.onServer || !CoreDBConfig.INSTANCE.enableChangelogOnUpdate || !CoreDBConfig.INSTANCE.showChangelogs) return;
if (!Reference.onServer || !CoreDBConfig.INSTANCE.enableChangelogOnUpdate || !CoreDBConfig.INSTANCE.showChangelogs)
return;
if (UpdateOverlay.isDownloading() || DownloaderManager.isRestartOnQueueFinish() || McIf.world() == null) return;
if (e.getNewClass() == ClassType.NONE) return;

Expand All @@ -284,7 +295,7 @@ public void onJoinLobby(WynnClassChangeEvent e) {
static BlockPos currentSpawn = null;

/**
* Block compass changing locations if a forced location is set
* Block compass changing locations if a forced location is set
*/
@SubscribeEvent
public void onCompassChange(PacketEvent<SPacketSpawnPosition> e) {
Expand Down Expand Up @@ -332,9 +343,13 @@ private static void startUpdateRegionName() {

FrameworkManager.getEventBus().post(new SchedulerEvent.RegionUpdate());

LocationData data = PlayerInfo.get(LocationData.class);
if (!data.inUnknownLocation()) {
String location = data.getLocation();
LocationData locationData = PlayerInfo.get(LocationData.class);
if (locationData == null) {
return;
}

if (!locationData.inUnknownLocation()) {
String location = locationData.getLocation();
if (!WebManager.getTerritories().containsKey(location)) {
location = location.replace('\'', '’');
}
Expand All @@ -348,7 +363,7 @@ private static void startUpdateRegionName() {

for (TerritoryProfile pf : WebManager.getTerritories().values()) {
if (pf.insideArea((int) pl.posX, (int) pl.posZ)) {
data.setLocation(pf.getFriendlyName());
locationData.setLocation(pf.getFriendlyName());
return;
}
}
Expand All @@ -358,23 +373,23 @@ private static void startUpdateRegionName() {

// housing instances are over these chunk coordinates
if (chunkX >= 4096 && chunkZ >= 4096) {
if (data.inHousing()) return;
if (locationData.inHousing()) return;

data.setLocation("Housing");
locationData.setLocation("Housing");
return;
}

// war arenas are below these chunk coordinates
if (chunkX <= -4096 && chunkZ <= -4096) {
if (data.inWars()) return;
if (locationData.inWars()) return;

data.setLocation("Wars");
locationData.setLocation("Wars");
return;
}

// none match, set to unknow
if (!data.inUnknownLocation()) {
data.setLocation("");
if (!locationData.inUnknownLocation()) {
locationData.setLocation("");
}

}, 0, 3, TimeUnit.SECONDS);
Expand Down
45 changes: 31 additions & 14 deletions src/main/java/com/wynntils/modules/core/managers/PartyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import com.wynntils.core.framework.instances.containers.PartyContainer;
import com.wynntils.core.framework.instances.data.SocialData;
import com.wynntils.core.utils.helpers.CommandResponse;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.util.text.ITextComponent;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -20,15 +22,20 @@ public class PartyManager {
private static final Pattern PROMOTE_PATTERN = Pattern.compile("Successfully promoted (.+) to party leader!");

private static final CommandResponse listExecutor = new CommandResponse("/party list", (matcher, text) -> {
PlayerInfo.get(SocialData.class).resetPlayerParty();

SocialData socialData = PlayerInfo.get(SocialData.class);
if (socialData == null) {
return;
}
socialData.resetPlayerParty();

String entire = matcher.group(0);
if (entire.contains("You must be in")) { // clears the party
PlayerInfo.get(SocialData.class).getPlayerParty().removeMember(McIf.player().getName());
socialData.getPlayerParty().removeMember(McIf.player().getName());
return;
}

PartyContainer partyContainer = PlayerInfo.get(SocialData.class).getPlayerParty();
PartyContainer partyContainer = socialData.getPlayerParty();
for (ITextComponent components : text.getSiblings()) {
if (McIf.getFormattedText(components).startsWith("§e")) continue;

Expand All @@ -47,36 +54,46 @@ public static void handlePartyList() {

public static void handleMessages(ITextComponent component) {
String unformattedText = McIf.getUnformattedText(component);
String formattedText = McIf.getFormattedText(component);

if (unformattedText.startsWith("You have successfully joined the party.")) {
handlePartyList();
return;
}
PartyContainer partyContainer = PlayerInfo.get(SocialData.class).getPlayerParty();

SocialData socialData = PlayerInfo.get(SocialData.class);
if (socialData == null) {
return;
}

EntityPlayerSP player = McIf.player();
PartyContainer partyContainer = socialData.getPlayerParty();

if (unformattedText.startsWith("You have been removed from the party.")
|| McIf.getUnformattedText(component).startsWith("Your party has been disbanded since you were the only member remaining.")
|| McIf.getUnformattedText(component).startsWith("Your party has been disbanded.")) {
PlayerInfo.get(SocialData.class).resetPlayerParty();
|| unformattedText.startsWith("Your party has been disbanded since you were the only member remaining.")
|| unformattedText.startsWith("Your party has been disbanded.")) {
socialData.resetPlayerParty();
return;
}
if (unformattedText.startsWith("You have successfully created a party.")) {
partyContainer.setOwner(McIf.player().getName());
partyContainer.addMember(McIf.player().getName());
partyContainer.setOwner(player.getName());
partyContainer.addMember(player.getName());
return;
}
if (McIf.getFormattedText(component).startsWith("§e") && McIf.getUnformattedText(component).contains("has joined the party.")) {
String member = McIf.getUnformattedText(component).split(" has joined the party.")[0];
if (formattedText.startsWith("§e") && unformattedText.contains("has joined the party.")) {
String member = unformattedText.split(" has joined the party.")[0];
partyContainer.addMember(member);
return;
}
if (McIf.getFormattedText(component).startsWith("§e") && McIf.getUnformattedText(component).contains("has left the party.")) {
if (formattedText.startsWith("§e") && unformattedText.contains("has left the party.")) {
handlePartyList();

String member = McIf.getUnformattedText(component).split(" has left the party.")[0];
String member = unformattedText.split(" has left the party.")[0];
partyContainer.removeMember(member);
return;
}
if (unformattedText.startsWith("You are now the leader of this party! Type /party for a list of commands.")) {
partyContainer.setOwner(McIf.player().getName());
partyContainer.setOwner(player.getName());
return;
}
Matcher promoteMatcher = PROMOTE_PATTERN.matcher(unformattedText);
Expand Down

0 comments on commit e1154a0

Please sign in to comment.