Skip to content

Instantly share code, notes, and snippets.

@Joedobo27
Created March 29, 2018 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joedobo27/ec0c7f4b9eb8ed43f6cdf37e42b0a9c8 to your computer and use it in GitHub Desktop.
Save Joedobo27/ec0c7f4b9eb8ed43f6cdf37e42b0a9c8 to your computer and use it in GitHub Desktop.
package com.joedobo27.mcl;
import com.joedobo27.libs.bytecode.BytecodeTools;
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtPrimitiveType;
import javassist.NotFoundException;
import javassist.bytecode.*;
import org.gotti.wurmunlimited.modloader.classhooks.HookManager;
import org.gotti.wurmunlimited.modloader.interfaces.Configurable;
import org.gotti.wurmunlimited.modloader.interfaces.Initable;
import org.gotti.wurmunlimited.modloader.interfaces.WurmServerMod;
import java.util.Properties;
import java.util.logging.Logger;
public class MailCountLimitMod implements WurmServerMod, Initable, Configurable {
private static int maxMailCount = 100;
static final Logger logger = Logger.getLogger(MailCountLimitMod.class.getName());
@Override
public void configure(Properties properties) {
maxMailCount = Integer.parseInt(properties.getProperty("maxMailCount", Integer.toString(maxMailCount)));
}
@Override
public void init() {
try {
String injectCode = "" +
"java.util.Set wurmMails1 = com.wurmonline.server.items.WurmMail.getMailsFor(pinf.wurmId);\n" +
"com.wurmonline.server.items.WurmMail[] wurmMails2 = new com.wurmonline.server.items.WurmMail[wurmMails1.size()];\n" +
"wurmMails1.toArray(wurmMails2);\n" +
"int inTransitCount = 0;\n" +
"for (int i=0; i<wurmMails2.length; i++) {\n" +
"\tcom.wurmonline.server.items.Item item = null;\n" +
"\ttry {\n" +
"\t\titem = com.wurmonline.server.Items.getItem(wurmMails2[i].itemId);\n" +
"\t} catch (com.wurmonline.server.NoSuchItemException ignore) {}\n" +
"\tif (item == null)\n" +
"\t\treturn;\n" +
"\tinTransitCount += item.getAllItems(false).length;\n" +
"\tinTransitCount ++;\n" +
"}\n" +
"if (inTransitCount + this.mailbox.getAllItems(false).length > "+maxMailCount+"){\n" +
"\tthis.getResponder().getCommunicator().sendNormalServerMessage(\n" +
"\t\t\tname + \" would have \" + java.lang.Integer.toString(inTransitCount + this.mailbox.getAllItems(false).length) +\n" +
"\t\t\t\t\t\" items in transit which exceed the max of \" + "+maxMailCount+");\n" +
"\treturn;\n" +
"}";
int insertLine = BytecodeTools.getInsertLineAfterMethod("com.wurmonline.server.questions.MailSendQuestion", new String[]{
"answer", "(Ljava/util/Properties;)V"}, Opcode.INVOKESTATIC, new String[]{"createPlayerInfo",
"(Ljava/lang/String;)Lcom/wurmonline/server/players/PlayerInfo;",
"com.wurmonline.server.players.PlayerInfoFactory"});
HookManager.getInstance().getClassPool().get("com.wurmonline.server.questions.MailSendQuestion")
.getMethod("answer", Descriptor.ofMethod(CtPrimitiveType.voidType, new CtClass[]{
HookManager.getInstance().getClassPool().get("java.util.Properties")})).insertAt(insertLine, injectCode);
}catch (BadBytecode | NotFoundException | CannotCompileException e){
logger.warning(e.getMessage());
}
}
}
# suppress inspection "UnusedProperty"
classname=com.joedobo27.mcl.MailCountLimitMod
# suppress inspection "UnusedProperty"
classpath=MailCountLimitMod.jar,libs/*.jar
maxMailCount=300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment