Skip to content

Commit

Permalink
Fixed blockstate bugs, fixed incompatibility with CodeChickenLib
Browse files Browse the repository at this point in the history
 - Fixes #150
 - Fixes #149
 - Fixes #148
 - Fixes #146
 - Fixes #143
 - Fixes #142
 - Fixes #149
 - Fixes #144
 - Fixes #145
  • Loading branch information
Runemoro committed Jan 4, 2019
1 parent 708ec7e commit b03f0b7
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 47 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ sourceSets {
}

minecraft {
version "1.12.2-14.23.4.2703"
version "1.12.2-14.23.5.2768"
runDir "run"
mappings "stable_39"
makeObfSourceJar false
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/dimdev/utils/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.dimdev.utils;

import java.util.Objects;

public class Pair<L, R> {
public final L left;
public final R right;

public Pair(L left, R right) {
this.left = left;
this.right = right;
}

@Override
public boolean equals(Object obj) {
return obj instanceof Pair &&
Objects.equals(((Pair<?, ?>) obj).left, left) &&
Objects.equals(((Pair<?, ?>) obj).right, right);
}

@Override
public int hashCode() {
return Objects.hashCode(left) + Objects.hashCode(right) * 31;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.dimdev.utils.LeftIdentityPair;
import org.dimdev.utils.Pair;

import javax.annotation.Nullable;
import java.util.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* An implementation of IBlockState which stores the properties in a bitfield rather
Expand All @@ -35,10 +38,10 @@
*/
@SuppressWarnings("deprecation")
public class NumericalBlockState extends BlockStateBase {
private static final Map<LeftIdentityPair<BlockStateContainer, Integer>, NumericalBlockState> blockStates = new HashMap<>(); // TODO: WeakHashMap?
private static final Map<LeftIdentityPair<IProperty<?>, Comparable<?>>, Integer> valueToNumber = new HashMap<>();
private static final Map<LeftIdentityPair<IProperty<?>, Integer>, Comparable<?>> numberToValue = new HashMap<>();
private static final Map<IProperty<?>, Integer> propertyWidths = new IdentityHashMap<>();
private static final Map<Pair<BlockStateContainer, Integer>, NumericalBlockState> blockStates = new HashMap<>(); // TODO: WeakHashMap?
private static final Map<Pair<IProperty<?>, Comparable<?>>, Integer> valueToNumber = new HashMap<>();
private static final Map<Pair<IProperty<?>, Integer>, Comparable<?>> numberToValue = new HashMap<>();
private static final Map<IProperty<?>, Integer> propertyWidths = new HashMap<>();

protected final BlockStateContainer container;
protected final Block block;
Expand All @@ -54,7 +57,7 @@ public static NumericalBlockState get(BlockStateContainer container, int data) {
// Getting it from a cache is necessary to make sure == between two NumericalBlockStates
// with the same container and data will work. The cache is shared for all containers
// to avoid the overhead of many small HashMaps.
LeftIdentityPair<BlockStateContainer, Integer> key = new LeftIdentityPair<>(container, data);
Pair<BlockStateContainer, Integer> key = new Pair<>(container, data);
NumericalBlockState blockState = blockStates.get(key);

if (blockState == null) {
Expand All @@ -71,7 +74,7 @@ public static NumericalBlockState fromPropertyValueMap(BlockStateContainer conta
int data = 0;
for (Map.Entry<IProperty<?>, Comparable<?>> entry : map.entrySet()) {
IProperty<?> property = entry.getKey();
data |= valueToNumber.get(new LeftIdentityPair<>(property, entry.getValue())) << offsets.get(property);
data |= valueToNumber.get(new Pair<>(property, entry.getValue())) << offsets.get(property);
}

return get(container, data);
Expand All @@ -88,8 +91,8 @@ public static <T extends Comparable<T>> void makePropertyInfo(IProperty<T> prope
// Fill the 'number -> value' and 'value -> number' maps
int i = 0;
for (T value : allowedValues) {
numberToValue.put(new LeftIdentityPair<>(property, i), value);
valueToNumber.put(new LeftIdentityPair<>(property, value), i);
numberToValue.put(new Pair<>(property, i), value);
valueToNumber.put(new Pair<>(property, value), i);
i++;
}
}
Expand All @@ -109,7 +112,7 @@ public <T extends Comparable<T>> T getValue(IProperty<T> property) {

int width = propertyWidths.get(property);
int number = data >>> offset & 0xFFFFFFFF >>> 32 - width;
Comparable<?> value = numberToValue.get(new LeftIdentityPair<>(property, number));
Comparable<?> value = numberToValue.get(new Pair<>(property, number));

return property.getValueClass().cast(value);
}
Expand All @@ -122,7 +125,7 @@ public <T extends Comparable<T>, V extends T> IBlockState withProperty(IProperty
throw new IllegalArgumentException("Cannot set property " + property + " as it does not exist in " + container);
}

int number = valueToNumber.get(new LeftIdentityPair<>(property, value));
int number = valueToNumber.get(new Pair<>(property, value));
int width = propertyWidths.get(property);
int mask = (0xFFFFFFFF >>> offset & 0xFFFFFFFF >>> 32 - width) << offset;
int newData = data & ~mask | number << offset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import net.minecraft.client.resources.IResource;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.client.model.ModelDynBucket;
import net.minecraftforge.client.model.ModelLoader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -25,8 +27,8 @@ public class DynamicTextureMap extends TextureMap {
private final Map<String, TextureAtlasSprite> loadedSprites = new ConcurrentHashMap<>();
private final List<TextureAtlasSprite> spritesNeedingUpload = new CopyOnWriteArrayList<>();
private final Lock spriteLoadingLock = new ReentrantLock();
private DynamicStitcher stitcher;
private boolean atlasNeedsExpansion;
private DynamicStitcher stitcher = null;
private boolean atlasNeedsExpansion = false;

public DynamicTextureMap(String basePath) {
super(basePath);
Expand Down Expand Up @@ -60,8 +62,11 @@ public void init() {
TextureUtil.allocateTextureImpl(getGlTextureId(), mipmapLevels, stitcher.getImageWidth(), stitcher.getImageHeight());
LOGGER.info("Created {}x{} '{}' atlas", stitcher.getImageWidth(), stitcher.getImageHeight(), basePath);

ForgeHooksClient.onTextureStitchedPre(this);
ForgeHooksClient.onTextureStitchedPost(this);
EventUtil.postEventAllowingErrors(new TextureStitchEvent.Pre(this));
ModelLoader.White.INSTANCE.register(this);
ModelDynBucket.LoaderDynBucket.INSTANCE.register(this);

EventUtil.postEventAllowingErrors(new TextureStitchEvent.Post(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.dimdev.vanillafix.dynamicresources;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.EventBus;
import net.minecraftforge.fml.common.eventhandler.IEventListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;

public class EventUtil {
private static final Logger LOGGER = LogManager.getLogger();

public static void postEventAllowingErrors(Event event) {
int busID;
try {
Field busIDField = EventBus.class.getDeclaredField("busID");
busIDField.setAccessible(true);
busID = (int) busIDField.get(MinecraftForge.EVENT_BUS);

This comment has been minimized.

Copy link
@Chocohead

Chocohead Jan 4, 2019

Why not Field#getInt rather than casting from Object?

This comment has been minimized.

Copy link
@Runemoro

Runemoro Jan 4, 2019

Author Member

Didn't realize that existed. I changed it to getInt in the latest commit.

} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}

IEventListener[] listeners = event.getListenerList().getListeners(busID);
for (IEventListener listener : listeners) {
try {
listener.invoke(event);
} catch (Throwable t) {
LOGGER.error(event + " listener '" + listener + "' threw exception, models may be broken", t);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.common.ForgeModContainer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.EventBus;
import net.minecraftforge.fml.common.eventhandler.IEventListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.vanillafix.dynamicresources.DynamicTextureMap;
import org.dimdev.vanillafix.dynamicresources.EventUtil;
import org.dimdev.vanillafix.dynamicresources.model.DynamicBakedModelProvider;
import org.dimdev.vanillafix.dynamicresources.model.DynamicModelProvider;
import org.dimdev.vanillafix.dynamicresources.model.ModelLocationInformation;
Expand All @@ -38,8 +33,6 @@ public class MixinModelManager {
@Shadow @Final private BlockModelShapes modelProvider;
@Shadow @Final private TextureMap texMap;

private static final Logger LOGGER = LogManager.getLogger();

/**
* @reason Don't set up the ModelLoader. Instead, set up the caching DynamicModelProvider
* and DynamicBakedModelProviders, which will act as the model registry.
Expand Down Expand Up @@ -81,29 +74,9 @@ public void onResourceManagerReload(IResourceManager resourceManager) {

// Post the event, but just log an error if a listener throws an exception. The ModelLoader is
// null, but very few mods use it. Custom support will be needed for those that do.
postEventAllowingErrors(new ModelBakeEvent((ModelManager) (Object) this, modelRegistry, null));
EventUtil.postEventAllowingErrors(new ModelBakeEvent((ModelManager) (Object) this, modelRegistry, null));

// Make the model provider load blockstate to model information. See MixinBlockModelShapes
modelProvider.reloadModels();
}

private static void postEventAllowingErrors(Event event) {
int busID;
try {
Field busIDField = EventBus.class.getDeclaredField("busID");
busIDField.setAccessible(true);
busID = (int) busIDField.get(MinecraftForge.EVENT_BUS);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}

IEventListener[] listeners = event.getListenerList().getListeners(busID);
for (IEventListener listener : listeners) {
try {
listener.invoke(event);
} catch (Throwable t) {
LOGGER.error(event + " listener '" + listener + "' threw exception, models may be broken", t);
}
}
}
}

0 comments on commit b03f0b7

Please sign in to comment.