Added the potion bracket handler
This commit is contained in:
parent
a78840cf02
commit
f0dd8bf8c0
3 changed files with 135 additions and 0 deletions
18
src/main/java/com/blamejared/api/potions/IPotion.java
Normal file
18
src/main/java/com/blamejared/api/potions/IPotion.java
Normal file
|
@ -0,0 +1,18 @@
|
|||
package com.blamejared.api.potions;
|
||||
|
||||
import stanhebben.zenscript.annotations.*;
|
||||
|
||||
@ZenClass("modtweaker.api.IPotion")
|
||||
public interface IPotion {
|
||||
|
||||
@ZenGetter("name")
|
||||
String name();
|
||||
|
||||
@ZenGetter("badEffect")
|
||||
boolean isBadEffect();
|
||||
|
||||
@ZenGetter("liquidColor")
|
||||
int getLiquidColor();
|
||||
|
||||
Object getInternal();
|
||||
}
|
33
src/main/java/com/blamejared/api/potions/MCPotion.java
Normal file
33
src/main/java/com/blamejared/api/potions/MCPotion.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package com.blamejared.api.potions;
|
||||
|
||||
import minetweaker.mc1112.brackets.EntityBracketHandler;
|
||||
import net.minecraft.potion.Potion;
|
||||
|
||||
public class MCPotion implements IPotion {
|
||||
|
||||
private final Potion potion;
|
||||
|
||||
public MCPotion(Potion potion) {
|
||||
this.potion = potion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return potion.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBadEffect() {
|
||||
return potion.isBadEffect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLiquidColor() {
|
||||
return potion.getLiquidColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getInternal() {
|
||||
return potion;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package com.blamejared.brackets;
|
||||
|
||||
import com.blamejared.api.potions.*;
|
||||
import minetweaker.*;
|
||||
import minetweaker.annotations.BracketHandler;
|
||||
import minetweaker.api.item.IngredientAny;
|
||||
import minetweaker.api.minecraft.MineTweakerMC;
|
||||
import minetweaker.mc1112.brackets.ItemBracketHandler;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.init.MobEffects;
|
||||
import net.minecraft.item.*;
|
||||
import net.minecraft.potion.Potion;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.expression.*;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.parser.Token;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.type.natives.IJavaMethod;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@BracketHandler(priority = 100)
|
||||
public class PotionBracketHandler implements IBracketHandler {
|
||||
|
||||
private static final Map<String, Potion> potionNames = new HashMap();
|
||||
private final IZenSymbol symbolAny = MineTweakerAPI.getJavaStaticFieldSymbol(IngredientAny.class, "INSTANCE");
|
||||
private final IJavaMethod method;
|
||||
|
||||
public PotionBracketHandler() {
|
||||
this.method = MineTweakerAPI.getJavaMethod(PotionBracketHandler.class, "getPotion", String.class);
|
||||
}
|
||||
|
||||
public static Map<String, Potion> getPotionNames() {
|
||||
return potionNames;
|
||||
}
|
||||
|
||||
public static void rebuildRegistry() {
|
||||
potionNames.clear();
|
||||
Potion.REGISTRY.getKeys().forEach(key ->{
|
||||
potionNames.put(key.getResourcePath(), Potion.getPotionFromResourceLocation(key.toString()));
|
||||
});
|
||||
}
|
||||
|
||||
public static IPotion getPotion(String name) {
|
||||
Potion pot = potionNames.get(name);
|
||||
System.out.println(potionNames);
|
||||
System.out.println(name);
|
||||
if(pot == null){
|
||||
return null;
|
||||
}
|
||||
return new MCPotion(pot);
|
||||
}
|
||||
|
||||
public IZenSymbol resolve(IEnvironmentGlobal environment, List<Token> tokens) {
|
||||
return tokens.size() > 2 && tokens.get(0).getValue().equals("potion") && tokens.get(1).getValue().equals(":") ? this.find(environment, tokens, 2, tokens.size()) : null;
|
||||
}
|
||||
|
||||
private IZenSymbol find(IEnvironmentGlobal environment, List<Token> tokens, int startIndex, int endIndex) {
|
||||
StringBuilder valueBuilder = new StringBuilder();
|
||||
|
||||
for(int i = startIndex; i < endIndex; ++i) {
|
||||
Token token = tokens.get(i);
|
||||
valueBuilder.append(token.getValue());
|
||||
}
|
||||
|
||||
return new PotionReferenceSymbol(environment, valueBuilder.toString());
|
||||
}
|
||||
|
||||
private class PotionReferenceSymbol implements IZenSymbol {
|
||||
|
||||
private final IEnvironmentGlobal environment;
|
||||
private final String name;
|
||||
|
||||
public PotionReferenceSymbol(IEnvironmentGlobal environment, String name) {
|
||||
this.environment = environment;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public IPartialExpression instance(ZenPosition position) {
|
||||
return new ExpressionCallStatic(position, this.environment, PotionBracketHandler.this.method, new ExpressionString(position, this.name));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue