Skip to content

Commit

Permalink
perf: 优化某些操作, 修复某些潜在错误
Browse files Browse the repository at this point in the history
1. ClientTickHandler.java 加入了可能会被引用的ClientPlayer对象
2. 加入了大量的@NotNull以及@nullable@Override注解
3. 为某些重要方法编写了Java doc
4. 修复PlayerMessageSender可能的多线程问题
5. VarDataHandler.java中为避免不正确调用, 加了一堆throw new UnsupportedOperationException();
6. 优化了ScriptVar的打开文件夹方式, 引用了Mc自己的代码, 现在可支持更多操作系统
7. 在几个Manager里头, 查找返回值可能为null的情况处, 改用了Optional对象以增加可读性
  • Loading branch information
winsrewu committed May 1, 2024
1 parent 8283f77 commit c3c282f
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 113 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/jawbts/noglerr/event/ClientTickHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import fi.dy.masa.malilib.interfaces.IClientTickHandler;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;

public class ClientTickHandler implements IClientTickHandler {
public static MinecraftClient mc;
public static ClientPlayerEntity player;

@Override
public void onClientTick(MinecraftClient mc) {
if (mc.world != null && mc.player != null) {
OnTick.onTick(mc);
}
ClientTickHandler.mc = mc;
player = mc.player;
}
}
13 changes: 7 additions & 6 deletions src/main/java/org/jawbts/noglerr/tweak/var/DataEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.reflect.TypeToken;
import com.google.gson.*;
import org.jawbts.noglerr.util.PlayerMessageSender;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.util.ArrayList;
Expand Down Expand Up @@ -80,21 +81,21 @@ public String getType() {
return "Unknown";
}

public void printDataList(PlayerMessageSender pms, int page) {
public void printDataList(@NotNull PlayerMessageSender pms, int page) {
pms.add("yellow", "------");
pms.add(String.format("%s List: ", getType()));

assert dataManagerBase != null;
List<SavedData> sd = dataManagerBase.getDataList();

int maxpage = sd.size() / 10;
page = Math.min(page - 1, maxpage);
int subpage = Math.min((page + 1) * 10, sd.size());
int maxPage = sd.size() / 10;
page = Math.min(page - 1, maxPage);
int subPage = Math.min((page + 1) * 10, sd.size());

pms.add(sd.subList(page * 10, subpage));
pms.add(sd.subList(page * 10, subPage));

if (sd.size() > 10) {
pms.add(String.format("Page %d / %d", page + 1, maxpage + 1));
pms.add(String.format("Page %d / %d", page + 1, maxPage + 1));
} else if (sd.isEmpty()) {
pms.add("Nothing...");
}
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/org/jawbts/noglerr/tweak/var/DataManagerBase.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
package org.jawbts.noglerr.tweak.var;

import java.util.List;
import java.util.Optional;

public interface DataManagerBase {
//if not exists, return false
/**
* 删除一个数据
*
* @param name 数据的名称
* @return 如果不存在, {@code false}, 反之为 {@code true}.
*/
boolean delData(String name);

//if exists, return false; if hard, modify the data
/**
* 添加一个数据
*
* @param name 数据的名称
* @param value 数据的值
* @param hard 是否强制添加
* @return 在不强制添加的情况下, 如果数据已存在, 返回{@code false}. 强制情况下一定为{@code true}.
*/
boolean addData(String name, String value, boolean hard);

//if not exists, return null
SavedData getData(String name);
/**
* 获取数据
*
* @param name 数据的名称
* @return 存在就有值, 不存在就没有.
*/
Optional<SavedData> getData(String name);

void setData(List<SavedData> savedDataList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
import net.minecraft.entity.Entity;
import org.jawbts.noglerr.client.NoglerrClient;

import java.util.List;

public class ScriptVarDataHandler extends VarDataHandler {
private final String name;

public ScriptVarDataHandler(String name) {
this.name = name;
}

public ScriptVarDataHandler() {
throw new UnsupportedOperationException();
}

public ScriptVarDataHandler(String name, String value) {
throw new UnsupportedOperationException();
}

@Override
public String getTreatedData(Entity entity, ClientWorld world, ClientPlayerEntity player) {
try {
Expand All @@ -20,4 +31,34 @@ public String getTreatedData(Entity entity, ClientWorld world, ClientPlayerEntit
return "EXCEPTION " + e.getMessage();
}
}

@Override
public String getData() {
throw new UnsupportedOperationException();
}

@Override
public void setData(String data) {
throw new UnsupportedOperationException();
}

@Override
public SavedData getSavedData() {
throw new UnsupportedOperationException();
}

@Override
public void setArgList(List<VarDataHandler.Arg> argList) {
throw new UnsupportedOperationException();
}

@Override
public void createArgList() {
throw new UnsupportedOperationException();
}

@Override
public String toString() {
throw new UnsupportedOperationException();
}
}
40 changes: 20 additions & 20 deletions src/main/java/org/jawbts/noglerr/tweak/var/TargetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class TargetManager implements DataManagerBase {
private static final TargetManager INSTANCE = new TargetManager();
Expand All @@ -20,46 +21,45 @@ public static DataEditor getEditor() {
}

public boolean addData(String name, String value, boolean hard) {
SavedData sd = getData(name);
if (sd != null) {
Optional<SavedData> sd = getData(name);
sd.ifPresentOrElse(savedData -> {
if (hard) {
targetDataHandlerList.remove(new TargetDataHandler(sd.name, sd.value));
targetDataHandlerList.remove(new TargetDataHandler(savedData.name, savedData.value));
targetDataHandlerList.add(new TargetDataHandler(name, value));
onChanged();
return true;
}
return false;
}
targetDataHandlerList.add(new TargetDataHandler(name, value));
onChanged();
return true;
}, () -> {
targetDataHandlerList.add(new TargetDataHandler(name, value));
onChanged();
});

return hard || sd.isEmpty();
}

public boolean delData(String name) {
onChanged();
SavedData sd = getData(name);
if (sd == null) {
return false;
}
targetDataHandlerList.remove(new TargetDataHandler(sd.name, sd.value));
return true;
Optional<SavedData> sd = getData(name);
sd.ifPresent(savedData -> {
targetDataHandlerList.remove(new TargetDataHandler(savedData.name, savedData.value));
onChanged();
});
return sd.isPresent();
}

public SavedData getData(String name) {
public Optional<SavedData> getData(String name) {
for (TargetDataHandler data : targetDataHandlerList) {
if (data.getName().equals(name)) {
return data.getSavedData();
return Optional.of(data.getSavedData());
}
}
return null;
return Optional.empty();
}

public void setData(List<SavedData> savedDataList) {
onChanged();
targetDataHandlerList.clear();
for (SavedData sd : savedDataList) {
targetDataHandlerList.add(new TargetDataHandler(sd.name, sd.value));
}
onChanged();
}

public List<SavedData> getDataList() {
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/org/jawbts/noglerr/tweak/var/TextManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class TextManager implements DataManagerBase {
private static final TextManager INSTANCE = new TextManager();
Expand All @@ -19,38 +20,37 @@ public static DataEditor getEditor() {
}

public boolean addData(String name, String value, boolean hard) {
SavedData sd = getData(name);
if (sd != null) {
Optional<SavedData> sd = getData(name);
sd.ifPresentOrElse(savedData -> {
if (hard) {
textDataHandlerList.remove(new TextDataHandler(sd.name, sd.value));
textDataHandlerList.remove(new TextDataHandler(savedData.name, savedData.value));
textDataHandlerList.add(new TextDataHandler(name, value));
onChanged();
return true;
}
return false;
}
textDataHandlerList.add(new TextDataHandler(name, value));
onChanged();
return true;
}, () -> {
textDataHandlerList.add(new TextDataHandler(name, value));
onChanged();
});

return hard || sd.isEmpty();
}

public boolean delData(String name) {
SavedData sd = getData(name);
if (sd == null) {
return false;
}
textDataHandlerList.remove(new TextDataHandler(sd.name, sd.value));
onChanged();
return true;
Optional<SavedData> sd = getData(name);
sd.ifPresent(savedData -> {
textDataHandlerList.remove(new TextDataHandler(savedData.name, savedData.value));
onChanged();
});
return sd.isPresent();
}

public SavedData getData(String name) {
public Optional<SavedData> getData(String name) {
for (TextDataHandler data : textDataHandlerList) {
if (data.getName().equals(name)) {
return data.getSavedData();
return Optional.of(data.getSavedData());
}
}
return null;
return Optional.empty();
}

public TextDataHandler getHandler(String name) {
Expand Down
Loading

0 comments on commit c3c282f

Please sign in to comment.