Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
EntityType Ingredient
  • Loading branch information
jaredlll08 committed Apr 25, 2021
1 parent 5db0e71 commit 0187f6e
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
@@ -0,0 +1,128 @@
package com.blamejared.crafttweaker.api.entity;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import com.blamejared.crafttweaker.impl.entity.MCEntityType;
import com.blamejared.crafttweaker.impl.tag.MCTagWithAmount;
import com.blamejared.crafttweaker_annotations.annotations.Document;
import net.minecraft.entity.EntityType;
import net.minecraft.tags.ITag;
import org.openzen.zencode.java.ZenCodeType;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* EntityIngredient that facilitates accepting either a single, or multiple {@link net.minecraft.entity.EntityType}s, {@link com.blamejared.crafttweaker.impl.tag.MCTag<net.minecraft.entity.EntityType>}s
* or {@link MCTagWithAmount<net.minecraft.entity.EntityType>}s.
*/
@ZenRegister
@ZenCodeType.Name("crafttweaker.api.entity.EntityIngredient")
@Document("vanilla/api/entity/EntityIngredient")
public abstract class CTEntityIngredient implements CommandStringDisplayable {

CTEntityIngredient() {}

public abstract String getCommandString();

public abstract <T> T mapTo(Function<EntityType<?>, T> typeMapper,
BiFunction<ITag<EntityType<?>>, Integer, T> tagMapper,
Function<Stream<T>, T> compoundMapper);

@ZenCodeType.Operator(ZenCodeType.OperatorType.OR)
public CTEntityIngredient asCompound(CTEntityIngredient other) {

List<CTEntityIngredient> ingredients = new ArrayList<>();
if(other instanceof CompoundEntityIngredient) {
ingredients.addAll(((CompoundEntityIngredient) other).elements);
} else {
ingredients.add(other);
}

if(this instanceof CompoundEntityIngredient) {
((CompoundEntityIngredient) this).elements.addAll(ingredients);
return this;
} else {
ingredients.add(this);
}

return new CompoundEntityIngredient(ingredients);
}

public static final class EntityTypeIngredient extends CTEntityIngredient {

final MCEntityType entityType;

public EntityTypeIngredient(MCEntityType entityType) {

this.entityType = entityType;
}

@Override
public String getCommandString() {

return entityType.getCommandString();
}

@Override
public <T> T mapTo(Function<EntityType<?>, T> typeMapper, BiFunction<ITag<EntityType<?>>, Integer, T> tagMapper, Function<Stream<T>, T> compoundMapper) {

return typeMapper.apply(entityType.getInternal());
}

}

public final static class EntityTagWithAmountIngredient extends CTEntityIngredient {

final MCTagWithAmount<MCEntityType> tag;

public EntityTagWithAmountIngredient(MCTagWithAmount<MCEntityType> tag) {

this.tag = tag;
}

@Override
public String getCommandString() {

return tag.getCommandString();
}

@Override
public <T> T mapTo(Function<EntityType<?>, T> typeMapper, BiFunction<ITag<EntityType<?>>, Integer, T> tagMapper, Function<Stream<T>, T> compoundMapper) {

return (T) tagMapper.apply(tag.getTag().getInternalRaw(), tag.getAmount());
}

}

public final static class CompoundEntityIngredient extends CTEntityIngredient {

final List<CTEntityIngredient> elements;

public CompoundEntityIngredient(List<CTEntityIngredient> elements) {

this.elements = elements;
}

@Override
public String getCommandString() {

return elements.stream().map(CTEntityIngredient::getCommandString).collect(Collectors.joining(" | "));
}

@Override
public <T> T mapTo(Function<EntityType<?>, T> typeMapper, BiFunction<ITag<EntityType<?>>, Integer, T> tagMapper, Function<Stream<T>, T> compoundMapper) {

Stream<T> stream = elements.stream()
.map(element -> element.mapTo(typeMapper, tagMapper, compoundMapper));
return compoundMapper.apply(stream);
}

}


}
Expand Up @@ -2,6 +2,8 @@

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.brackets.CommandStringDisplayable;
import com.blamejared.crafttweaker.api.entity.CTEntityIngredient;
import com.blamejared.crafttweaker.api.fluid.CTFluidIngredient;
import com.blamejared.crafttweaker_annotations.annotations.Document;
import com.blamejared.crafttweaker_annotations.annotations.ZenWrapper;
import net.minecraft.entity.Entity;
Expand All @@ -11,6 +13,8 @@
import org.openzen.zencode.java.ZenCodeType;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@ZenRegister
Expand Down Expand Up @@ -126,4 +130,17 @@ public int hashCode() {
return getInternal().hashCode();
}

@ZenCodeType.Caster(implicit = true)
public CTEntityIngredient asEntityIngredient(){
return new CTEntityIngredient.EntityTypeIngredient(this);
}

@ZenCodeType.Operator(ZenCodeType.OperatorType.OR)
public CTEntityIngredient asList(CTEntityIngredient other) {
List<CTEntityIngredient> elements = new ArrayList<>();
elements.add(asEntityIngredient());
elements.add(other);
return new CTEntityIngredient.CompoundEntityIngredient(elements);
}

}
@@ -0,0 +1,35 @@
package com.blamejared.crafttweaker.impl.tag.expansions;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.entity.CTEntityIngredient;
import com.blamejared.crafttweaker.impl.entity.MCEntityType;
import com.blamejared.crafttweaker.impl.tag.MCTag;
import com.blamejared.crafttweaker.impl.tag.MCTagWithAmount;
import com.blamejared.crafttweaker_annotations.annotations.Document;
import org.openzen.zencode.java.ZenCodeType;

import java.util.ArrayList;
import java.util.List;

@ZenRegister
@Document("vanilla/api/tags/ExpandEntityTag")
@ZenCodeType.Expansion("crafttweaker.api.tag.MCTag<crafttweaker.api.entity.MCEntityType>")
public class ExpandEntityTag {

@ZenCodeType.Method
@ZenCodeType.Caster(implicit = true)
public static CTEntityIngredient asIngredient(MCTag<MCEntityType> _this) {

return new CTEntityIngredient.EntityTagWithAmountIngredient(new MCTagWithAmount<>(_this, 1));
}

@ZenCodeType.Operator(ZenCodeType.OperatorType.OR)
public static CTEntityIngredient asList(MCTag<MCEntityType> _this, CTEntityIngredient other) {

List<CTEntityIngredient> elements = new ArrayList<>();
elements.add(asIngredient(_this));
elements.add(other);
return new CTEntityIngredient.CompoundEntityIngredient(elements);
}

}
@@ -0,0 +1,34 @@
package com.blamejared.crafttweaker.impl.tag.expansions;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.entity.CTEntityIngredient;
import com.blamejared.crafttweaker.impl.entity.MCEntityType;
import com.blamejared.crafttweaker.impl.tag.MCTagWithAmount;
import com.blamejared.crafttweaker_annotations.annotations.Document;
import org.openzen.zencode.java.ZenCodeType;

import java.util.ArrayList;
import java.util.List;

@ZenRegister
@Document("vanilla/api/tags/ExpandEntityTagWithAmount")
@ZenCodeType.Expansion("crafttweaker.api.tag.MCTagWithAmount<crafttweaker.api.entity.MCEntityType>")
public class ExpandEntityTagWithAmount {

@ZenCodeType.Method
@ZenCodeType.Caster(implicit = true)
public static CTEntityIngredient asIngredient(MCTagWithAmount<MCEntityType> _this) {

return new CTEntityIngredient.EntityTagWithAmountIngredient(_this);
}

@ZenCodeType.Operator(ZenCodeType.OperatorType.OR)
public static CTEntityIngredient asList(MCTagWithAmount<MCEntityType> _this, CTEntityIngredient other) {

List<CTEntityIngredient> elements = new ArrayList<>();
elements.add(asIngredient(_this));
elements.add(other);
return new CTEntityIngredient.CompoundEntityIngredient(elements);
}

}

0 comments on commit 0187f6e

Please sign in to comment.