-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea86662
commit fede8c4
Showing
14 changed files
with
282 additions
and
295 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package mods.railcraft.api.carts; | ||
|
||
public interface NeedsFuel { | ||
|
||
boolean needsFuel(); | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/main/java/mods/railcraft/util/routing/RoutingStatementParser.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,40 @@ | ||
package mods.railcraft.util.routing; | ||
|
||
import java.util.regex.Pattern; | ||
import java.util.regex.PatternSyntaxException; | ||
import org.jetbrains.annotations.Nullable; | ||
import mods.railcraft.Translations; | ||
|
||
public class RoutingStatementParser { | ||
|
||
public static final String REGEX_SYMBOL = "\\?"; | ||
|
||
public static ParsedStatement parse(String keyword, boolean supportsRegex, String line) | ||
throws RoutingLogicException { | ||
String keywordMatch = keyword + REGEX_SYMBOL + "?="; | ||
if (!line.matches(keywordMatch + ".*")) { | ||
throw new RoutingLogicException(Translations.RoutingTable.UNRECOGNIZED_KEYWORD, line); | ||
} | ||
var isRegex = line.matches(keyword + REGEX_SYMBOL + "=.*"); | ||
if (!supportsRegex && isRegex) { | ||
throw new RoutingLogicException(Translations.RoutingTable.ERROR_UNSUPPORTED_REGEX, line); | ||
} | ||
var value = line.replaceFirst(keywordMatch, ""); | ||
Pattern pattern = null; | ||
if (isRegex) { | ||
try { | ||
pattern = Pattern.compile(line); | ||
} catch (PatternSyntaxException ex) { | ||
throw new RoutingLogicException(Translations.RoutingTable.ERROR_INVALID_REGEX, line); | ||
} | ||
} | ||
return new ParsedStatement(value, pattern); | ||
} | ||
|
||
public record ParsedStatement(String value, @Nullable Pattern pattern) { | ||
|
||
public boolean isRegex() { | ||
return this.pattern != null; | ||
} | ||
} | ||
} |
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
43 changes: 22 additions & 21 deletions
43
src/main/java/mods/railcraft/util/routing/expression/condition/DestCondition.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 |
---|---|---|
@@ -1,32 +1,33 @@ | ||
package mods.railcraft.util.routing.expression.condition; | ||
|
||
import java.util.function.Predicate; | ||
import org.apache.commons.lang3.StringUtils; | ||
import mods.railcraft.api.carts.Routable; | ||
import mods.railcraft.util.routing.RouterBlockEntity; | ||
import mods.railcraft.util.routing.RoutingLogicException; | ||
import net.minecraft.world.entity.vehicle.AbstractMinecart; | ||
import mods.railcraft.util.routing.RoutingStatementParser; | ||
import mods.railcraft.util.routing.expression.Expression; | ||
|
||
public class DestCondition extends ParsedCondition { | ||
public class DestCondition { | ||
|
||
public DestCondition(String line) throws RoutingLogicException { | ||
super("Dest", true, line); | ||
} | ||
public static final String KEYWORD = "Dest"; | ||
|
||
@Override | ||
public boolean evaluate(RouterBlockEntity routerBlockEntity, AbstractMinecart cart) { | ||
if (cart instanceof Routable routableCart) { | ||
String cartDest = routableCart.getDestination(); | ||
if (StringUtils.equalsIgnoreCase("null", value)) { | ||
return StringUtils.isBlank(cartDest); | ||
} | ||
if (StringUtils.isBlank(cartDest)) { | ||
return false; | ||
} | ||
if (isRegex) { | ||
return cartDest.matches(value); | ||
public static Expression parse(String line) throws RoutingLogicException { | ||
var statement = RoutingStatementParser.parse(KEYWORD, true, line); | ||
Predicate<String> predicate = statement.isRegex() | ||
? statement.pattern().asMatchPredicate() | ||
: s -> s.startsWith(statement.value()); | ||
return (router, minecart) -> { | ||
if (minecart instanceof Routable routable) { | ||
var destination = routable.getDestination(); | ||
if (StringUtils.equalsIgnoreCase("null", statement.value())) { | ||
return StringUtils.isBlank(destination); | ||
} | ||
if (StringUtils.isBlank(destination)) { | ||
return false; | ||
} | ||
return predicate.test(destination); | ||
} | ||
return cartDest.startsWith(value); | ||
} | ||
return false; | ||
return false; | ||
}; | ||
} | ||
} |
43 changes: 21 additions & 22 deletions
43
src/main/java/mods/railcraft/util/routing/expression/condition/LocomotiveCondition.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 |
---|---|---|
@@ -1,33 +1,32 @@ | ||
package mods.railcraft.util.routing.expression.condition; | ||
|
||
import mods.railcraft.util.routing.RouterBlockEntity; | ||
import java.util.Locale; | ||
import java.util.function.Predicate; | ||
import mods.railcraft.util.routing.RoutingLogicException; | ||
import mods.railcraft.util.routing.RoutingStatementParser; | ||
import mods.railcraft.util.routing.expression.Expression; | ||
import mods.railcraft.world.entity.RailcraftEntityTypes; | ||
import mods.railcraft.world.entity.vehicle.locomotive.Locomotive; | ||
import net.minecraft.world.entity.vehicle.AbstractMinecart; | ||
|
||
public class LocomotiveCondition extends ParsedCondition { | ||
public class LocomotiveCondition { | ||
|
||
public LocomotiveCondition(String line) throws RoutingLogicException { | ||
super("Loco", false, line); | ||
} | ||
public static final String KEYWORD = "Loco"; | ||
|
||
@Override | ||
public boolean evaluate(RouterBlockEntity routerBlockEntity, AbstractMinecart cart) { | ||
if (cart instanceof Locomotive loco) { | ||
if (value.equalsIgnoreCase("Electric")) { | ||
return loco.getType() == RailcraftEntityTypes.ELECTRIC_LOCOMOTIVE.get(); | ||
} | ||
if (value.equalsIgnoreCase("Steam")) { | ||
return loco.getType() == RailcraftEntityTypes.STEAM_LOCOMOTIVE.get(); | ||
} | ||
if (value.equalsIgnoreCase("Creative")) { | ||
return loco.getType() == RailcraftEntityTypes.CREATIVE_LOCOMOTIVE.get(); | ||
public static Expression parse(String line) throws RoutingLogicException { | ||
var statement = RoutingStatementParser.parse(KEYWORD, false, line); | ||
Predicate<Locomotive> predicate = switch (statement.value().toLowerCase(Locale.ROOT)) { | ||
case "electric" -> loco -> loco.getType() == RailcraftEntityTypes.ELECTRIC_LOCOMOTIVE.get(); | ||
case "steam" -> loco -> loco.getType() == RailcraftEntityTypes.STEAM_LOCOMOTIVE.get(); | ||
case "creative" -> loco -> loco.getType() == RailcraftEntityTypes.CREATIVE_LOCOMOTIVE.get(); | ||
case "none" -> null; | ||
default -> throw new IllegalArgumentException("Unexpected value: " + statement.value()); | ||
}; | ||
return (router, minecart) -> { | ||
if (minecart instanceof Locomotive loco && predicate != null) { | ||
return predicate.test(loco); | ||
} | ||
if (value.equalsIgnoreCase("None")) { | ||
return false; | ||
} | ||
} | ||
return value.equalsIgnoreCase("None"); | ||
return true; | ||
|
||
}; | ||
} | ||
} |
30 changes: 13 additions & 17 deletions
30
src/main/java/mods/railcraft/util/routing/expression/condition/NameCondition.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 |
---|---|---|
@@ -1,25 +1,21 @@ | ||
package mods.railcraft.util.routing.expression.condition; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import mods.railcraft.util.routing.RouterBlockEntity; | ||
import java.util.function.Predicate; | ||
import mods.railcraft.util.routing.RoutingLogicException; | ||
import net.minecraft.world.entity.vehicle.AbstractMinecart; | ||
import mods.railcraft.util.routing.RoutingStatementParser; | ||
import mods.railcraft.util.routing.expression.Expression; | ||
|
||
public class NameCondition extends ParsedCondition { | ||
public class NameCondition { | ||
|
||
public NameCondition(String line) throws RoutingLogicException { | ||
super("Name", true, line); | ||
} | ||
public static final String KEYWORD = "Name"; | ||
|
||
@Override | ||
public boolean evaluate(RouterBlockEntity routerBlockEntity, AbstractMinecart cart) { | ||
if (!cart.hasCustomName()) { | ||
return StringUtils.equalsIgnoreCase("null", value); | ||
} | ||
String customName = cart.getDisplayName().getString(); | ||
if (isRegex) { | ||
return customName.matches(value); | ||
} | ||
return StringUtils.equalsIgnoreCase(customName, value); | ||
public static Expression parse(String line) throws RoutingLogicException { | ||
var statement = RoutingStatementParser.parse(KEYWORD, true, line); | ||
Predicate<String> predicate = statement.isRegex() | ||
? statement.pattern().asMatchPredicate() | ||
: statement.value()::equalsIgnoreCase; | ||
return (router, minecart) -> minecart.hasCustomName() | ||
? predicate.test(minecart.getCustomName().getString()) | ||
: statement.value().equalsIgnoreCase("null"); | ||
} | ||
} |
24 changes: 10 additions & 14 deletions
24
src/main/java/mods/railcraft/util/routing/expression/condition/OwnerCondition.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 |
---|---|---|
@@ -1,23 +1,19 @@ | ||
package mods.railcraft.util.routing.expression.condition; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import mods.railcraft.util.routing.RouterBlockEntity; | ||
import mods.railcraft.util.routing.RoutingLogicException; | ||
import mods.railcraft.util.routing.RoutingStatementParser; | ||
import mods.railcraft.util.routing.expression.Expression; | ||
import mods.railcraft.world.entity.vehicle.CartTools; | ||
import net.minecraft.world.entity.vehicle.AbstractMinecart; | ||
|
||
public class OwnerCondition extends ParsedCondition { | ||
public class OwnerCondition { | ||
|
||
public OwnerCondition(String line) throws RoutingLogicException { | ||
super("Owner", false, line); | ||
} | ||
public static final String KEYWORD = "Owner"; | ||
|
||
@Override | ||
public boolean evaluate(RouterBlockEntity routerBlockEntity, AbstractMinecart cart) { | ||
var owner = CartTools.getCartOwner(cart); | ||
if (owner == null) { | ||
return false; | ||
} | ||
return StringUtils.equalsIgnoreCase(value, owner.getName()); | ||
public static Expression parse(String line) throws RoutingLogicException { | ||
var statement = RoutingStatementParser.parse(KEYWORD, false, line); | ||
return (router, minecart) -> { | ||
var owner = CartTools.getCartOwner(minecart); | ||
return owner != null && statement.value().equalsIgnoreCase(owner.getName()); | ||
}; | ||
} | ||
} |
Oops, something went wrong.