Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
EntityType Ingredient
- Loading branch information
1 parent
5db0e71
commit 0187f6e
Showing
4 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
src/main/java/com/blamejared/crafttweaker/api/entity/CTEntityIngredient.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,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); | ||
} | ||
|
||
} | ||
|
||
|
||
} |
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/blamejared/crafttweaker/impl/tag/expansions/ExpandEntityTag.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,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); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/blamejared/crafttweaker/impl/tag/expansions/ExpandEntityTagWithAmount.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,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); | ||
} | ||
|
||
} |