Skip to content

Commit

Permalink
More work on ITems
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlich committed Apr 23, 2020
1 parent bd702d5 commit db9184a
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
docsOut/
run/
29 changes: 7 additions & 22 deletions src/main/java/com/blamejared/contenttweaker/VanillaFactory.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,45 @@
package com.blamejared.contenttweaker;

import com.blamejared.contenttweaker.items.*;
import com.blamejared.contenttweaker.items.wrappers.*;
import com.blamejared.crafttweaker.api.*;
import com.blamejared.crafttweaker.api.actions.*;
import com.blamejared.crafttweaker.api.annotations.*;
import com.blamejared.crafttweaker.api.logger.*;
import com.blamejared.crafttweaker.impl.util.*;
import net.minecraft.item.*;
import net.minecraft.util.*;
import net.minecraftforge.fml.*;
import net.minecraftforge.registries.*;
import org.openzen.zencode.java.*;

@ZenRegister
@ZenCodeType.Name("mods.contenttweaker.VanillaFactory")
public class VanillaFactory {

public static boolean registerLocked = false;
public static IForgeRegistry<Item> registry = null;

@ZenCodeType.Method
public static void registerItem(MCItemProperties properties, String name){
registerItem(new MCItemRepresentation(properties), name);
}

@ZenCodeType.Method
public static void registerItem(MCItemRepresentation representation, String name) {
registerItem(representation, new MCResourceLocation(new ResourceLocation(ContentTweaker.MOD_ID, name)));
}

@ZenCodeType.Method
public static void registerItem(MCItemRepresentation representation, MCResourceLocation resourceLocation) {
public static void registerItem(CoTItem item, MCResourceLocation resourceLocation) {
CraftTweakerAPI.apply(new IAction() {
@Override
public void apply() {
ForgeRegistries.ITEMS.register(new CoTItem(representation).setRegistryName(resourceLocation
.getInternal()));
ForgeRegistries.ITEMS.register(item.setRegistryName(resourceLocation.getInternal()));
}

@Override
public String describe() {
return String.format("Registering item %s with resource location %s", representation
.toString(), resourceLocation.getCommandString());
return String.format("Registering item %s with resource location %s", item, resourceLocation
.getCommandString());
}

@Override
public boolean validate(ILogger logger) {
if(registry == null){
if(registry == null) {
logger.error("Registering items too early");
logger.error("Ignoring Registration for item " + item);
return false;
}

if(registerLocked) {
logger.error("Cannot register items after setupCommon!");
logger.error("Ignoring Registration for item " + representation.toString());
logger.error("Ignoring Registration for item " + item);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.blamejared.contenttweaker.items.wrappers.*;
import com.blamejared.crafttweaker.api.annotations.*;
import net.minecraft.item.*;
import net.minecraftforge.common.*;
import org.openzen.zencode.java.*;

import java.util.*;
Expand All @@ -11,6 +12,7 @@
@ZenCodeType.Name("mods.contenttweaker.BracketHandlers")
public class BracketHandlers {

@ZenCodeType.Method
@BracketResolver("itemgroup")
public static MCItemGroup getItemGroup(String tokens) {
return Arrays.stream(ItemGroup.GROUPS)
Expand All @@ -19,4 +21,10 @@ public static MCItemGroup getItemGroup(String tokens) {
.findAny()
.orElseThrow(() -> new IllegalArgumentException("Could not find itemgroup for '<itemgroup:" + tokens + ">'!"));
}

@ZenCodeType.Method
@BracketResolver("tooltype")
public static MCToolType getToolType(String tokens) {
return new MCToolType(ToolType.get(tokens));
}
}
60 changes: 58 additions & 2 deletions src/main/java/com/blamejared/contenttweaker/items/CoTItem.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
package com.blamejared.contenttweaker.items;

import com.blamejared.contenttweaker.items.functions.*;
import com.blamejared.contenttweaker.items.wrappers.*;
import com.blamejared.crafttweaker.impl.blocks.*;
import com.blamejared.crafttweaker.impl.entity.player.*;
import com.blamejared.crafttweaker.impl.item.*;
import mcp.*;
import net.minecraft.block.*;
import net.minecraft.entity.*;
import net.minecraft.entity.player.*;
import net.minecraft.inventory.*;
import net.minecraft.item.*;
import net.minecraft.util.math.*;
import net.minecraft.world.*;
import net.minecraftforge.common.*;

import javax.annotation.*;
import java.util.*;


@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class CoTItem extends Item {

public CoTItem(MCItemRepresentation representation) {
super(representation.getProperties().createInternal());
private final Map<ToolType, ToolTypeFunction> toolClasses;
private final ToolDestroySpeedFunction speedFunction;

public CoTItem(MCItemProperties properties) {
super(properties.createInternal());
this.toolClasses = properties.getToolClasses();
this.speedFunction = properties.destroySpeedFunction;
}

@Override
public int getHarvestLevel(ItemStack stack, ToolType tool, @Nullable PlayerEntity player, @Nullable BlockState blockState) {

if(!toolClasses.containsKey(tool)) {
return -1;
}
final MCPlayerEntity playerEntity = player != null ? new MCPlayerEntity(player) : null;
final MCBlockState state = blockState != null ? new MCBlockState(blockState) : null;
return toolClasses.get(tool)
.getHarvestLevel(new MCItemStackMutable(stack), new MCToolType(tool), playerEntity, state);
}

@Override
public float getDestroySpeed(ItemStack stack, BlockState state) {
return speedFunction.getDestroySpeed(new MCItemStackMutable(stack), new MCBlockState(state));
}

@Override
public Set<ToolType> getToolTypes(ItemStack stack) {
return toolClasses.keySet();
}

@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, BlockState state, BlockPos pos, LivingEntity entityLiving) {
if(getMaxDamage(stack) == 0) {
return false;
}

if(!worldIn.isRemote && state.getBlockHardness(worldIn, pos) != 0.0F) {
stack.damageItem(1, entityLiving, (livingEntity) -> {
livingEntity.sendBreakAnimation(EquipmentSlotType.MAINHAND);
});
}

return true;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.blamejared.contenttweaker.items.expand;

import com.blamejared.contenttweaker.items.wrappers.*;
import com.blamejared.crafttweaker.api.annotations.*;
import com.blamejared.crafttweaker.impl.blocks.*;
import org.openzen.zencode.java.*;

@ZenRegister
@ZenCodeType.Expansion("crafttweaker.api.block.MCBlockState")
public class ExpandMCBlockState {

@ZenCodeType.Getter("harvestTool")
@ZenCodeType.Method
public static MCToolType getHarvestTool(MCBlockState _this) {
return new MCToolType(_this.getInternal().getHarvestTool());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.blamejared.contenttweaker.items.functions;

import com.blamejared.crafttweaker.api.annotations.*;
import com.blamejared.crafttweaker.api.item.*;
import com.blamejared.crafttweaker.impl.blocks.*;
import com.blamejared.crafttweaker.impl.item.*;
import org.openzen.zencode.java.*;

@ZenRegister
@FunctionalInterface
@ZenCodeType.Name("mods.contenttweaker.item.ToolDestroySpeedFunction")
public interface ToolDestroySpeedFunction {

@ZenCodeType.Method
float getDestroySpeed(IItemStack stack, MCBlockState state);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.blamejared.contenttweaker.items.functions;

import com.blamejared.contenttweaker.items.wrappers.*;
import com.blamejared.crafttweaker.api.annotations.*;
import com.blamejared.crafttweaker.api.item.*;
import com.blamejared.crafttweaker.impl.blocks.*;
import com.blamejared.crafttweaker.impl.entity.player.*;
import org.openzen.zencode.java.*;

@ZenRegister
@FunctionalInterface
@ZenCodeType.Name("mods.contenttweaker.item.ToolTypeFunction")
public interface ToolTypeFunction {
@ZenCodeType.Method
int getHarvestLevel(IItemStack stack, MCToolType type, @ZenCodeType.Nullable MCPlayerEntity playerEntity, @ZenCodeType.Nullable MCBlockState state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@ZenRegister
@ZenCodeType.Name("mods.contenttweaker.item.MCItemGroup")
@ZenWrapper(wrappedClass = "net.minecraftforge.event.entity.player.CriticalHitEvent", displayStringFormat = "%s.toString()")
@ZenWrapper(wrappedClass = "net.minecraft.item.ItemGroup", displayStringFormat = "%s.getCommandString()")
public class MCItemGroup implements CommandStringDisplayable {

private final ItemGroup internal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,92 @@
package com.blamejared.contenttweaker.items.wrappers;

import com.blamejared.contenttweaker.*;
import com.blamejared.contenttweaker.items.*;
import com.blamejared.contenttweaker.items.functions.*;
import com.blamejared.crafttweaker.api.annotations.*;
import com.blamejared.crafttweaker.impl.food.*;
import com.blamejared.crafttweaker.impl.util.*;
import net.minecraft.item.*;
import net.minecraft.util.*;
import net.minecraftforge.common.*;
import org.openzen.zencode.java.*;

import java.util.*;

@ZenRegister
@ZenCodeType.Name("mods.contenttweaker.item.MCItemProperties")
public class MCItemProperties {

private final Map<ToolType, ToolTypeFunction> toolClasses = new HashMap<>();
@ZenCodeType.Field
public MCItemGroup itemGroup = new MCItemGroup(ItemGroup.SEARCH);

@ZenCodeType.Field
public int maxStackSize = 64;

@ZenCodeType.Field
public int maxDamage = 0;

@ZenCodeType.Field
public MCFood food;

@ZenCodeType.Field
public boolean canRepair = true;

@ZenCodeType.Field
public ToolDestroySpeedFunction destroySpeedFunction = (stack, state) -> 1.0f;

@ZenCodeType.Constructor
public MCItemProperties() {
}

public Map<ToolType, ToolTypeFunction> getToolClasses() {
return toolClasses;
}

@ZenCodeType.Method
public MCItemProperties setMaxStackSize(int maxStackSize) {
public MCItemProperties withMaxStackSize(int maxStackSize) {
this.maxStackSize = maxStackSize;
return this;
}

@ZenCodeType.Method
public MCItemProperties setMaxDamage(int maxDamage) {
public MCItemProperties withMaxDamage(int maxDamage) {
this.maxDamage = maxDamage;
return this;
}

@ZenCodeType.Method
public MCItemProperties setFood(MCFood food) {
public MCItemProperties withFood(MCFood food) {
this.food = food;
return this;
}

@ZenCodeType.Method
public MCItemProperties setCanRepair(boolean canRepair) {
public MCItemProperties withCanRepair(boolean canRepair) {
this.canRepair = canRepair;
return this;
}

@ZenCodeType.Method
public MCItemProperties isTool(MCToolType type, ToolTypeFunction fun) {
toolClasses.put(type.getInternal(), fun);
return this;
}

@ZenCodeType.Method
public MCItemProperties isTool(MCToolType type, int harvestLevel) {
return isTool(type, (stack, type1, player, blockState) -> harvestLevel);
}

@ZenCodeType.Method
public MCItemProperties withDestroySpeed(ToolDestroySpeedFunction fun) {
destroySpeedFunction = fun;
return this;
}

@ZenCodeType.Method
public void build(String name) {
VanillaFactory.registerItem(new CoTItem(this), new MCResourceLocation(new ResourceLocation(ContentTweaker.MOD_ID, name)));
}

public Item.Properties createInternal() {
Item.Properties properties = new Item.Properties()
.group(itemGroup.getInternal())
Item.Properties properties = new Item.Properties().group(itemGroup.getInternal())
.maxStackSize(maxStackSize);

if(maxDamage != 0) {
Expand Down

0 comments on commit db9184a

Please sign in to comment.