replace all occurences of printStackTrace with loggers/better handling

This commit is contained in:
asie 2019-03-01 18:45:27 +01:00
parent 384184eff7
commit b50903e883
4 changed files with 80 additions and 67 deletions
src/main/java/net/fabricmc/fabric

View file

@ -25,11 +25,14 @@ import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
import net.minecraft.network.Packet;
import net.minecraft.util.Identifier;
import net.minecraft.util.PacketByteBuf;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.nio.charset.StandardCharsets;
import java.util.*;
public abstract class PacketRegistryImpl implements PacketRegistry {
protected static final Logger LOGGER = LogManager.getLogger();
protected final Map<Identifier, PacketConsumer> consumerMap;
PacketRegistryImpl() {
@ -81,6 +84,43 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
return toPacket(id, buf);
}
private boolean acceptRegisterType(Identifier id, PacketContext context, PacketByteBuf buf) {
Collection<Identifier> ids = new HashSet<>();
{
StringBuilder sb = new StringBuilder();
char c;
while (buf.readerIndex() < buf.writerIndex()) {
c = (char) buf.readByte();
if (c == 0) {
String s = sb.toString();
if (!s.isEmpty()) {
ids.add(new Identifier(s));
}
sb = new StringBuilder();
} else {
sb.append(c);
}
}
String s = sb.toString();
if (!s.isEmpty()) {
ids.add(new Identifier(s));
}
}
Collection<Identifier> target = getIdCollectionFor(context);
if (id.equals(PacketTypes.UNREGISTER)) {
target.removeAll(ids);
onReceivedUnregisterPacket(context, ids);
} else {
target.addAll(ids);
onReceivedRegisterPacket(context, ids);
}
return false; // continue execution for other mods
}
/**
* Hook for accepting packets used in Fabric mixins.
*
@ -91,40 +131,7 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
*/
public boolean accept(Identifier id, PacketContext context, PacketByteBuf buf) {
if (id.equals(PacketTypes.REGISTER) || id.equals(PacketTypes.UNREGISTER)) {
Collection<Identifier> ids = new HashSet<>();
{
StringBuilder sb = new StringBuilder();
char c;
while (buf.readerIndex() < buf.writerIndex()) {
c = (char) buf.readByte();
if (c == 0) {
String s = sb.toString();
if (!s.isEmpty()) {
ids.add(new Identifier(s));
}
sb = new StringBuilder();
} else {
sb.append(c);
}
}
String s = sb.toString();
if (!s.isEmpty()) {
ids.add(new Identifier(s));
}
}
Collection<Identifier> target = getIdCollectionFor(context);
if (id.equals(PacketTypes.UNREGISTER)) {
target.removeAll(ids);
onReceivedUnregisterPacket(context, ids);
} else {
target.addAll(ids);
onReceivedRegisterPacket(context, ids);
}
return false; // continue execution for other mods
return acceptRegisterType(id, context, buf);
}
PacketConsumer consumer = consumerMap.get(id);
@ -132,8 +139,7 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
try {
consumer.accept(context, buf);
} catch (Throwable t) {
// TODO: handle better
t.printStackTrace();
LOGGER.warn("Failed to handle packet " + id + "!", t);
}
return true;
} else {

View file

@ -20,4 +20,8 @@ public class RemapException extends Exception {
public RemapException(String message) {
super(message);
}
public RemapException(String message, Throwable t) {
super(message, t);
}
}

View file

@ -144,7 +144,7 @@ public class ModNioResourcePack extends AbstractFilenameResourcePack implements
}
});
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("findResources at " + path + " in namespace " + namespace + ", mod " + modInfo.getId() + " failed!", e);
}
}
}
@ -192,7 +192,7 @@ public class ModNioResourcePack extends AbstractFilenameResourcePack implements
return namespaces;
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("getNamespaces in mod " + modInfo.getId() + " failed!", e);
return Collections.emptySet();
}
}

View file

@ -37,33 +37,26 @@ import java.io.IOException;
@Mixin(WorldSaveHandler.class)
public class MixinWorldSaveHandler {
private static final int ID_REGISTRY_BACKUPS = 3;
private static final int FABRIC_ID_REGISTRY_BACKUPS = 3;
@Shadow
private static Logger LOGGER;
@Shadow
public File worldDir;
private CompoundTag lastSavedIdMap = null;
private boolean readWorldIdMap(File file) {
try {
if (file.exists()) {
FileInputStream fileInputStream = new FileInputStream(file);
CompoundTag tag = NbtIo.readCompressed(fileInputStream);
fileInputStream.close();
if (tag != null) {
RegistrySyncManager.apply(tag, RemappableRegistry.RemapMode.AUTHORITATIVE);
return true;
}
private CompoundTag fabric_lastSavedIdMap = null;
private boolean fabric_readIdMapFile(File file) throws IOException, RemapException {
if (file.exists()) {
FileInputStream fileInputStream = new FileInputStream(file);
CompoundTag tag = NbtIo.readCompressed(fileInputStream);
fileInputStream.close();
if (tag != null) {
RegistrySyncManager.apply(tag, RemappableRegistry.RemapMode.AUTHORITATIVE);
return true;
}
return false;
} catch (IOException e) {
return false;
} catch (RemapException e) {
e.printStackTrace();
return false;
}
return false;
}
private File getWorldIdMapFile(int i) {
@ -74,19 +67,29 @@ public class MixinWorldSaveHandler {
@Inject(method = "readProperties", at = @At("HEAD"))
public void readWorldProperties(CallbackInfoReturnable<LevelProperties> callbackInfo) {
// Load
for (int i = 0; i < ID_REGISTRY_BACKUPS; i++) {
LOGGER.info("Loading Fabric registry [file " + (i + 1) + "/" + (ID_REGISTRY_BACKUPS + 1) + "]");
if (readWorldIdMap(getWorldIdMapFile(i))) {
break;
for (int i = 0; i < FABRIC_ID_REGISTRY_BACKUPS; i++) {
LOGGER.info("Loading Fabric registry [file " + (i + 1) + "/" + (FABRIC_ID_REGISTRY_BACKUPS + 1) + "]");
try {
if (fabric_readIdMapFile(getWorldIdMapFile(i))) {
break;
}
} catch (IOException e) {
if (i >= FABRIC_ID_REGISTRY_BACKUPS - 1) {
throw new RuntimeException(e);
} else {
LOGGER.warn("Reading registry file failed!", e);
}
} catch (RemapException e) {
throw new RuntimeException("Remapping world failed!", e);
}
}
CompoundTag newIdMap = RegistrySyncManager.toTag(false);
if (!newIdMap.equals(lastSavedIdMap)) {
for (int i = ID_REGISTRY_BACKUPS - 1; i >= 0; i--) {
if (!newIdMap.equals(fabric_lastSavedIdMap)) {
for (int i = FABRIC_ID_REGISTRY_BACKUPS - 1; i >= 0; i--) {
File file = getWorldIdMapFile(i);
if (file.exists()) {
if (i == ID_REGISTRY_BACKUPS - 1) {
if (i == FABRIC_ID_REGISTRY_BACKUPS - 1) {
file.delete();
} else {
File target = getWorldIdMapFile(i + 1);
@ -100,10 +103,10 @@ public class MixinWorldSaveHandler {
NbtIo.writeCompressed(newIdMap, fileOutputStream);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("Failed to save registry file!", e);
}
lastSavedIdMap = newIdMap;
fabric_lastSavedIdMap = newIdMap;
}
}
}