Skip to content

Commit

Permalink
refactor: 4.0 beta-1
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Mar 12, 2024
1 parent 9d5422e commit 2d52fff
Show file tree
Hide file tree
Showing 34 changed files with 1,132 additions and 922 deletions.
22 changes: 13 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>tech.brenoepic</groupId>
<groupId>io.github.brenoepics</groupId>
<artifactId>MentionPlugin</artifactId>
<version>3.0</version>
<version>4.0</version>
<build>
<plugins>
<plugin>
Expand All @@ -23,7 +23,7 @@
<configuration>
<archive>
<manifest>
<mainClass>tech.brenoepic.MentionPlugin</mainClass>
<mainClass>io.github.brenoepics.MentionPlugin</mainClass>
</manifest>
</archive>
<descriptorRefs>
Expand All @@ -39,7 +39,6 @@
</goals>
</execution>
</executions>

</plugin>
</plugins>
</build>
Expand All @@ -62,14 +61,19 @@
<dependency>
<groupId>com.googlecode.owasp-java-html-sanitizer</groupId>
<artifactId>owasp-java-html-sanitizer</artifactId>
<version>20211018.2</version>
<version>20220608.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.brenoepics</groupId>
<artifactId>discord-webhooks</artifactId>
<version>0.2.1</version>
<scope>compile</scope>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.github.brenoepics</groupId>
<artifactId>MentionExtractor</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package tech.brenoepic;
package io.github.brenoepics;

import tech.brenoepic.core.MentionManager;
import tech.brenoepic.events.EmulatorEvents;
import io.github.brenoepics.core.MentionManager;
import io.github.brenoepics.events.EmulatorEvents;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.plugin.EventListener;
import com.eu.habbo.plugin.HabboPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.brenoepic.core.types.timeout.MentionTimeout;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;

Expand All @@ -17,9 +16,11 @@
* The main class for the MentionPlugin.
* Implements the EventListener interface and extends HabboPlugin.
*/
@Slf4j
public class MentionPlugin extends HabboPlugin implements EventListener {
public static final Logger LOGGER = LoggerFactory.getLogger(MentionPlugin.class);
public static MentionPlugin INSTANCE = null;
@Getter
private static MentionPlugin instance = null;
@Getter
private static MentionManager manager;

/**
Expand All @@ -28,24 +29,15 @@ public class MentionPlugin extends HabboPlugin implements EventListener {
*/
@Override
public void onEnable() {
INSTANCE = this;
instance = this;
Emulator.getPluginManager().registerEvents(this, new EmulatorEvents());
manager = new MentionManager(new ArrayList<>());
}

/**
* Called when the plugin is disabled.
*/
public void onDisable() {
}

/**
* Returns the MentionManager.
* @return MentionManager manager.
*/
public static MentionManager getManager() {
return manager;
}
/** Called when the plugin is disabled. */
public void onDisable() {
// TODO document why this method is empty
}

/**
* Returns a boolean indicating if the given Habbo has the given permission.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.github.brenoepics.commands;

import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.commands.Command;
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.users.Habbo;

/**
* Class representing the `BlockMentionCommand` command. It is used to block or unblock mentions for
* a user.
*
* @author BrenoEpic#9671
*/
public class BlockMentionCommand extends Command {

/**
* Constructor for the `BlockMentionCommand` command. It takes in the permission and command keys
* needed to execute the command.
*
* @param permission the permission required to execute the command
* @param keys the command keys used to execute the command
*/
public BlockMentionCommand(final String permission, final String[] keys) {
super(permission, keys);
}

/**
* Handles the execution of the `BlockMentionCommand` command.
*
* @param gameClient the game client that sent the command
* @param strings any additional parameters passed with the command
* @return a boolean indicating if the command was successfully handled
*/
public boolean handle(final GameClient gameClient, final String[] strings) {
Habbo habbo = gameClient.getHabbo();
final boolean blockMention = (boolean) habbo.getHabboStats().cache.get("blockmention");
habbo.whisper(getMessage(blockMention));
habbo.getHabboStats().cache.put("blockmention", !blockMention);

return true;
}

private static String getMessage(boolean blockMention) {
return Emulator.getTexts()
.getValue(
!blockMention ? "commands.cmd_mention.success.on" : "commands.cmd_mention.success.off");
}
}
138 changes: 138 additions & 0 deletions src/main/java/io/github/brenoepics/core/MentionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package io.github.brenoepics.core;

import static io.github.brenoepics.utils.Functions.buildPattern;

import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.users.Habbo;
import io.github.brenoepics.MentionExtractor;
import io.github.brenoepics.core.modes.*;
import io.github.brenoepics.core.types.HandleResult;
import io.github.brenoepics.core.types.IMention;
import io.github.brenoepics.core.types.exception.ExceptionHandler;
import io.github.brenoepics.core.types.exception.MentionException;
import io.github.brenoepics.logging.Message;
import java.util.*;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
* MentionManager class is responsible for handling all the mentions in the MentionPlugin. It holds
* all the mention modes in a LinkedHashMap where the key is the mention mode name and the value is
* an object of the IMention type. The class also holds a list of all the messages that are sent
* using the mention plugin.
*/
@Slf4j
public class MentionManager {
private final LinkedHashMap<String, IMention> mentionModes;
private final MentionExtractor extract;
@Getter private final List<Message> messages;

/**
* Constructor for MentionManager.
*
* @param messages List of all messages sent using the mention plugin.
*/
public MentionManager(List<Message> messages) {
this.messages = messages;
this.mentionModes = new LinkedHashMap<>();
this.extract = buildPattern();

this.mentionModes.put("everyone", new MentionEveryone("acc_mention_everyone", "everyone"));
this.mentionModes.put("here", new MentionHere("acc_mention_here", "here"));
this.mentionModes.put("room", new MentionRoom("acc_mention_room", "room"));
this.mentionModes.put("friends", new MentionFriends("acc_mention_friends", "friends"));
this.mentionModes.put("user", new MentionUser("acc_mention", "user"));
log.info("[MentionPlugin] successfully loaded with {} modes!", this.mentionModes.size());
}

/**
* Method to handle the mentions.
*
* @param sender The sender of the message.
* @param message The message sent.
* @return Returns a boolean indicating whether the message should be deleted or not.
*/
public boolean handle(Habbo sender, String message) {
Collection<IMention> iMentions = this.mentionModes.values();
Set<String> mentioned = extract.fromString(message);
if (mentioned.isEmpty()) return false;

ArrayList<IMention> mentions = getCollected(sender, iMentions);
for (IMention mode : mentions) {
HandleResult res = handleMentionList(sender, mentioned, message, mode);
if (!res.getError().isEmpty()) {
sender.whisper(res.getError());
}
if (res.isHandle()) {
return res.isDelete();
}
}
return false;
}

private static ArrayList<IMention> getCollected(Habbo sender, Collection<IMention> iMentions) {
return iMentions.stream()
.filter(mode -> sender.hasPermission(mode.getPermission(), mode.allowRoomOwner()))
.collect(Collectors.toCollection(ArrayList::new));
}

private HandleResult handleMentionList(
Habbo sender, Set<String> mentioned, String message, IMention mode) {
HandleResult result = new HandleResult(false, false);
for (String mention : mentioned) {
tryHandle(sender, message, mode, mention, result);
}

return result;
}

private void tryHandle(
Habbo sender, String message, IMention mode, String mention, HandleResult result) {
try {
HandleResult res = executeMention(sender, message, mode, mention);
if (res.isHandle()) {
result.setHandle(true);
if (res.isDelete()) {
result.setDelete(true);
}
}
} catch (MentionException e) {
if (Emulator.getConfig().getBoolean("commands.cmd_mention.message_error.delete")) {
result.setDelete(true);
}
result.setError(ExceptionHandler.handle(e));
}
}

private HandleResult executeMention(Habbo sender, String message, IMention mode, String receiver)
throws MentionException {
boolean delete = false;
boolean handle = false;
if (mode.execute(sender, receiver, message)) {
delete = handleSuccess(sender, mode, receiver, message);
handle = true;
}

return new HandleResult(delete, handle);
}


private boolean handleSuccess(Habbo sender, IMention mode, String receiver, String message) {
int timeout = Emulator.getConfig().getInt("mentionplugin.timeout_" + mode.getPrefix(), 20);
mode.getTimer().ofUser(sender, timeout);
this.addMessage(new Message(sender.getHabboInfo().getId(), receiver, message));

sender.whisper(Emulator.getTexts().getValue("commands.cmd_mention.message.sent"));

return Emulator.getConfig().getBoolean("commands.cmd_mention.message_success.delete");
}

public void disposeMessages() {
this.messages.clear();
}

public void addMessage(Message message) {
messages.add(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tech.brenoepic.core;
package io.github.brenoepics.core;

import com.eu.habbo.Emulator;

Expand Down
Loading

0 comments on commit 2d52fff

Please sign in to comment.