Skip to content

Commit

Permalink
feat: add a part of file about mapping module
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolLoong committed Sep 1, 2023
1 parent 6ecfb0f commit 9105270
Show file tree
Hide file tree
Showing 81 changed files with 126,942 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ buildSrc/**
/output/
/Allay-Server/src/test/resources/allayworld/db/
/output/
/cache/
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
package cn.allay.mapping.generator.id;

public class IdentifierMapper {
import org.apache.commons.lang3.tuple.Pair;

public interface IdentifierMapper {
Pair<String, Pair<String, Object>> translateIdentifier(String fullIdentifier);

default String getStateValue(String fullIdentifier, String property) {
String[] states = fullIdentifier.substring(fullIdentifier.lastIndexOf("[") + 1).replace("]", "").split(",");
for (String state : states) {
String key = state.split("=")[0];
if (property.equals(key)) {
return state.split("=")[1];
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.allay.mapping.generator.id;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(value = RetentionPolicy.RUNTIME)
public @interface IdentifierRemapper {
/**
* Regex string to check for when remapping states for blocks. Leave
* empty to check against all blocks.
*
* @return regex string to check against when remapping states for blocks
*/
String[] blockRegex() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cn.allay.mapping.generator.id.type;

import cn.allay.mapping.generator.id.IdentifierMapper;
import cn.allay.mapping.generator.id.IdentifierRemapper;
import org.apache.commons.lang3.tuple.Pair;


@IdentifierRemapper(blockRegex = ".*_planks$")
public class PlanksType implements IdentifierMapper {
@Override
public Pair<String, Pair<String, Object>> translateIdentifier(String fullIdentifier) {
return Pair.of("minecraft:planks", null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cn.allay.mapping.generator.state;

import org.apache.commons.lang3.tuple.Pair;

public abstract class StateMapper<T> {

/**
* Translates a state string from a Minecraft: Java Edition
* block state to it's bedrock counterpart.
*
* @param fullIdentifier the full identifier of the block to translate the state for
* @param value the value given in the Java blockstate
* @return a state string for bedrock edition
*/
public abstract Pair<String, T> translateState(String fullIdentifier, String value);

public static <V> V asType(Pair<String, ?> pair, Class<V> type) {
if (type.isInstance(pair.getValue())) {
return type.cast(pair.getValue());
}
throw new IllegalArgumentException("Mapping type " + pair.getValue().getClass().getSimpleName() + " cannot be cast to " + type.getSimpleName());
}

protected String getStateValue(String fullIdentifier, String property) {
String[] states = fullIdentifier.substring(fullIdentifier.lastIndexOf("[") + 1).replace("]", "").split(",");
for (String state : states) {
String key = state.split("=")[0];
if (property.equals(key)) {
return state.split("=")[1];
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cn.allay.mapping.generator.state;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(value = RetentionPolicy.RUNTIME)
public @interface StateRemapper {

/**
* Regex string to check for when remapping states for blocks. Leave
* empty to check against all blocks.
*
* @return regex string to check against when remapping states for blocks
*/
String[] blockRegex() default "";

/**
* The name of the Minecraft: Java Edition blockstate data.
*
* @return the name of the Minecraft: java edition blockstate data.
*/
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "axis", blockRegex = "minecraft:(stripped_)?bamboo_block")
public class BambooBlockAxisMapper extends StateMapper<String> {

@Override
public Pair<String, String> translateState(String fullIdentifier, String value) {
return Pair.of("pillar_axis", value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "axis", blockRegex = ".*basalt.?$")
public class BasaltAxisMapper extends StateMapper<String> {

@Override
public Pair<String, String> translateState(String fullIdentifier, String value) {
return Pair.of("pillar_axis", value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "facing", blockRegex = "^minecraft:bee_nest|^minecraft:beehive")
public class BeeNestDirectionMapper extends StateMapper<Integer> {

@Override
public Pair<String, Integer> translateState(String fullIdentifier, String value) {
int direction = switch (value) {
case "west" -> 1;
case "north" -> 2;
case "east" -> 3;
default -> 0;
};
return Pair.of("direction", direction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "facing", blockRegex = "^minecraft:big_dripleaf|^minecraft:big_dripleaf_stem")
public class BigDripleafDirectionMapper extends StateMapper<Integer> {

@Override
public Pair<String, Integer> translateState(String fullIdentifier, String value) {
return Pair.of("direction", switch (value) {
case "south" -> 0;
case "west" -> 1;
case "north" -> 2;
case "east" -> 3;
default -> throw new RuntimeException("Could not determine big dripleaf direction!");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "tilt", blockRegex = "^minecraft:big_dripleaf")
public class BigDripleafTiltMapper extends StateMapper<String> {

@Override
public Pair<String, String> translateState(String fullIdentifier, String value) {
return Pair.of("big_dripleaf_tilt", switch (value) {
case "none" -> "none";
case "unstable" -> "unstable";
case "partial" -> "partial_tilt";
case "full" -> "full_tilt";
default -> throw new RuntimeException("Unknown tilt state for big dripleaf!");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "face", blockRegex = ".*button.?$")
public class ButtonFaceMapper extends StateMapper<Integer> {

@Override
public Pair<String, Integer> translateState(String fullIdentifier, String value) {
String facing = this.getStateValue(fullIdentifier, "facing");
int facingDirection = switch (value) {
case "floor" -> 1;
case "wall" -> switch (facing) {
case "north" -> 2;
case "south" -> 3;
case "west" -> 4;
case "east" -> 5;
default -> 0;
};
default -> 0;
};
return Pair.of("facing_direction", facingDirection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "powered", blockRegex = ".*button.?$")
public class ButtonPoweredMapper extends StateMapper<Boolean> {

@Override
public Pair<String, Boolean> translateState(String fullIdentifier, String value) {
return Pair.of("button_pressed_bit", Boolean.parseBoolean(value));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "facing", blockRegex = "minecraft:calibrated_sculk_sensor")
public class CalibratedSculkSensorFacingMapper extends StateMapper<Integer> {

@Override
public Pair<String, Integer> translateState(String fullIdentifier, String value) {
int direction = switch (value) {
case "south" -> 0;
case "west" -> 1;
case "north" -> 2;
case "east" -> 3;
default -> throw new IllegalArgumentException("Got " + value + " instead of a cardinal direction");
};
return Pair.of("direction", direction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "facing", blockRegex = ".*campfire.?$")
public class CampfireFacingMapper extends StateMapper<Integer> {

@Override
public Pair<String, Integer> translateState(String fullIdentifier, String value) {
int direction = switch (value) {
case "north" -> 2;
case "west" -> 1;
case "east" -> 3;
default -> 0;
};
return Pair.of("direction", direction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "lit", blockRegex = ".*campfire.?$")
public class CampfireLitMapper extends StateMapper<Boolean> {

@Override
public Pair<String, Boolean> translateState(String fullIdentifier, String value) {
return Pair.of("extinguished", value.equals("false"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "candles", blockRegex = ".*candle$")
public class CandleCountMapper extends StateMapper<Integer> {

@Override
public Pair<String, Integer> translateState(String fullIdentifier, String value) {
// Java index starts at 1; Bedrock index starts at 0
return Pair.of("candles", Integer.parseInt(value) - 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "lit", blockRegex = ".*candle.*")
public class CandleLitMapper extends StateMapper<Boolean> {

@Override
public Pair<String, Boolean> translateState(String fullIdentifier, String value) {
return Pair.of("lit", value.equals("true"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "level", blockRegex = ".*cauldron.?$")
public class CauldronLiquidLevelMapper extends StateMapper<Integer> {
@Override
public Pair<String, Integer> translateState(String fullIdentifier, String value) {
return Pair.of("fill_level", switch (value) {
case "1" -> 3;
case "2" -> 4;
case "3" -> 6;
default -> throw new RuntimeException("Unknown cauldron liquid level!");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.allay.mapping.generator.state.type;

import cn.allay.mapping.generator.state.StateMapper;
import cn.allay.mapping.generator.state.StateRemapper;
import org.apache.commons.lang3.tuple.Pair;


@StateRemapper(value = "axis", blockRegex = "^minecraft:chain")
public class ChainAxisMapper extends StateMapper<String> {

@Override
public Pair<String, String> translateState(String fullIdentifier, String value) {
return Pair.of("pillar_axis", value);
}
}
Loading

0 comments on commit 9105270

Please sign in to comment.