Skip to content

Instantly share code, notes, and snippets.

@tyhdefu
Created May 31, 2020 14:01
Show Gist options
  • Save tyhdefu/d07515c1121e6bdc6217d850197b3d9f to your computer and use it in GitHub Desktop.
Save tyhdefu/d07515c1121e6bdc6217d850197b3d9f to your computer and use it in GitHub Desktop.
package org.spongepowered.common.mixin.api.mcp.map;
import com.flowpowered.math.vector.Vector2i;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.storage.MapDecoration;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.persistence.DataFormats;
import org.spongepowered.api.map.decoration.MapDecorationType;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.map.decoration.SpongeMapDecoration;
import org.spongepowered.common.registry.type.map.MapDecorationRegistryModule;
import org.spongepowered.common.util.Constants;
import java.io.IOException;
import java.util.UUID;
import static com.google.common.base.Preconditions.checkState;
@Mixin(MapDecoration.class)
public class MapDecorationMixin_API implements SpongeMapDecoration {
@Shadow @Final private MapDecoration.Type type;
@Shadow private byte x;
@Shadow private byte y;
@Shadow private byte rotation;
// If should save to disk
private boolean isPersistent;
//
private String key = Constants.Map.DECORATION_KEY_PREFIX + UUID.randomUUID().toString();
{
// All of the below types have no reason to be saved to disk
// This is because they can/should be calculated when needed
// Furthermore if a sponge plugin adds a MapDecoration, isPersistent
// should also be changed to true
switch (type) {
case PLAYER:
case PLAYER_OFF_MAP:
case PLAYER_OFF_LIMITS:
case FRAME: {
this.isPersistent = false;
break;
}
default: {
this.isPersistent = true;
break;
}
}
}
@Override
public MapDecorationType getType() {
return MapDecorationRegistryModule.getByMcType(type)
.orElseThrow(() -> new IllegalStateException("Tried to get MapDecoration type but it didn't exist in Sponge's registries! Have MC Decoration types been missed?"));
}
@Override
public Vector2i getPosition() {
return new Vector2i(this.x, this.y);
}
@Override
public int getX() {
return this.x;
}
@Override
public int getY() {
return this.y;
}
@Override
public void setPosition(Vector2i position) {
checkState(isInBounds(position.getX()), "x position out of bounds");
checkState(isInBounds(position.getY()), "y position out of bounds");
this.x = (byte) position.getX();
this.y = (byte) position.getY();
}
@Override
public void setX(int x) {
checkState(isInBounds(x), "x out of bounds");
this.x = (byte) x;
}
@Override
public void setY(int y) {
checkState(isInBounds(y), "y out of bounds");
this.y = (byte)y;
}
private boolean isInBounds(int i) {
return i >= 0 && i < Constants.Map.MAP_PIXELS;
}
@Override
public void setPersistent(boolean persistent) {
this.isPersistent = persistent;
}
@Override
public boolean isPersistent() {
return this.isPersistent;
}
@Override
public NBTTagCompound getMCNBT() {
try {
return net.minecraft.nbt.JsonToNBT.getTagFromJson(DataFormats.JSON.write(this.toContainer()));
} catch (NBTException | IOException e) {
// I don't see this ever happening but lets put logging in anyway
throw new IllegalStateException("Error converting DataView to MC NBT", e);
}
}
@Override
public void setKey(String key) {
this.key = key;
}
@Override
public String getKey() {
return this.key;
}
@Override
public int getContentVersion() {
return 0;
}
@Override
public DataContainer toContainer() {
return DataContainer.createNew()
.set(Constants.Map.DECORATION_TYPE, this.type.getIcon())
.set(Constants.Map.DECORATION_ID, this.key)
.set(Constants.Map.DECORATION_X, this.x)
.set(Constants.Map.DECORATION_Y, this.y)
.set(Constants.Map.DECORATION_ROTATION, this.rotation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment