generated from makamys/forge-mod-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature that warns about DataWatcher ID conflicts
See makamys/DMod#9
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/makamys/coretweaks/diagnostics/DataWatcherMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package makamys.coretweaks.diagnostics; | ||
|
||
import static makamys.coretweaks.CoreTweaks.LOGGER; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
|
||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
|
||
import net.minecraft.entity.DataWatcher; | ||
|
||
public class DataWatcherMonitor { | ||
private static WeakHashMap<DataWatcher, Map<Integer, List<AdditionRecord>>> data = new WeakHashMap<>(); | ||
|
||
public static void onAddition(DataWatcher dw, String entityClassName, int id) { | ||
AdditionRecord record = new AdditionRecord(); | ||
List<AdditionRecord> recordsForId = data.computeIfAbsent(dw, k -> new HashMap<>()).computeIfAbsent(id, k -> new ArrayList<>()); | ||
recordsForId.add(record); | ||
|
||
if(recordsForId.size() > 1) { | ||
LOGGER.warn("Detected DataWatcher ID conflict at ID " + id + " for entity " + entityClassName); | ||
for(int i = 0; i < recordsForId.size(); i++) { | ||
LOGGER.warn("Stack trace for registration #" + (i + 1) + " (out of " + recordsForId.size() + "): "); | ||
LOGGER.warn(recordsForId.get(i).stackTrace); | ||
} | ||
} | ||
} | ||
|
||
public static class AdditionRecord { | ||
public final String stackTrace; | ||
public AdditionRecord() { | ||
stackTrace = ExceptionUtils.getStackTrace(new Throwable()); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...java/makamys/coretweaks/mixin/diagnostics/detectdatawatcherconflict/MixinDataWatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package makamys.coretweaks.mixin.diagnostics.detectdatawatcherconflict; | ||
|
||
import static makamys.coretweaks.CoreTweaks.LOGGER; | ||
|
||
import java.util.Map; | ||
|
||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import makamys.coretweaks.Config; | ||
import makamys.coretweaks.diagnostics.DataWatcherMonitor; | ||
import net.minecraft.entity.DataWatcher; | ||
import net.minecraft.entity.Entity; | ||
|
||
@Mixin(DataWatcher.class) | ||
public class MixinDataWatcher { | ||
@Shadow | ||
private Entity field_151511_a; | ||
@Shadow | ||
private Map watchedObjects; | ||
|
||
@Inject(method = "addObject", at = @At("HEAD")) | ||
public void monitorObjectAddition(int id, Object object, CallbackInfo ci) { | ||
if(Config.detectDataWatcherIdConflictCulprit && field_151511_a != null) { | ||
DataWatcherMonitor.onAddition((DataWatcher)(Object)this, field_151511_a.getClass().getName(), id); | ||
} | ||
} | ||
|
||
@Inject(method = "addObjectByDataType", at = @At("HEAD")) | ||
public void monitorTypedObjectAddition(int id, int type, CallbackInfo ci) { | ||
boolean printedCulprit = false; | ||
if(Config.detectDataWatcherIdConflictCulprit && field_151511_a != null) { | ||
printedCulprit = true; | ||
DataWatcherMonitor.onAddition((DataWatcher)(Object)this, field_151511_a.getClass().getName(), id); | ||
} | ||
if(watchedObjects.containsKey(id)) { | ||
LOGGER.warn("Detected duplicate DataWatcher object registration for entity " + (field_151511_a == null ? "null" : field_151511_a.getClass().getName()) + " at already occupied ID " + id + ". Things are likely going to break!" + (!Config.detectDataWatcherIdConflictCulprit ? " Enable `detectDataWatcherIdConflictCulprit` to gather more information." : "")); | ||
if(!printedCulprit) LOGGER.warn("Last registration stack trace:\n" + ExceptionUtils.getStackTrace(new Throwable())); | ||
} | ||
} | ||
} |