Skip to content

Commit

Permalink
Add Block property manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlll08 committed May 8, 2021
1 parent 656f13c commit 1ebd4c3
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 2 deletions.
@@ -0,0 +1,73 @@
package com.blamejared.crafttweaker.impl.actions.blocks;

import com.blamejared.crafttweaker.api.actions.IUndoableAction;
import com.blamejared.crafttweaker.impl_native.blocks.ExpandBlock;
import net.minecraft.block.Block;
import net.minecraftforge.fml.LogicalSide;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class ActionSetBlockProperty<T> implements IUndoableAction {

private final Block block;
private final String propertyName;
private final T newValue;
private final T oldValue;
private final Consumer<T> valueSetter;
private Function<T, String> valueNameGetter;

public ActionSetBlockProperty(Block block, String propertyName, T newValue, T oldValue, Consumer<T> valueSetter) {

this.block = block;
this.propertyName = propertyName;
this.newValue = newValue;
this.oldValue = oldValue;
this.valueSetter = valueSetter;
this.valueNameGetter = Object::toString;
}

public ActionSetBlockProperty(Block block, String propertyName, T newValue, T oldValue, Consumer<T> valueSetter, Function<T, String> valueNameGetter) {

this.block = block;
this.propertyName = propertyName;
this.newValue = newValue;
this.oldValue = oldValue;
this.valueSetter = valueSetter;
this.valueNameGetter = valueNameGetter;
}

@Override
public void apply() {

this.valueSetter.accept(newValue);
}

@Override
public String describe() {

return "Set the value of " + propertyName + " on " + ExpandBlock.getCommandString(block) + " to: '" + this.valueNameGetter
.apply(newValue) + "'";
}

@Override
public void undo() {

this.valueSetter.accept(oldValue);
}

@Override
public String describeUndo() {

return "Reset the value of " + propertyName + " on " + ExpandBlock.getCommandString(block) + " to: '" + this.valueNameGetter
.apply(oldValue) + "'";
}

@Override
public boolean shouldApplyOn(LogicalSide side) {

return shouldApplySingletons();
}

}
@@ -1,12 +1,16 @@
package com.blamejared.crafttweaker.impl_native.blocks;

import com.blamejared.crafttweaker.api.CraftTweakerAPI;
import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.impl.actions.blocks.ActionSetBlockProperty;
import com.blamejared.crafttweaker.impl.tag.MCTag;
import com.blamejared.crafttweaker.impl.tag.manager.TagManagerBlock;
import com.blamejared.crafttweaker.impl_native.block.material.ExpandMaterial;
import com.blamejared.crafttweaker_annotations.annotations.Document;
import com.blamejared.crafttweaker_annotations.annotations.NativeTypeRegistration;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.tags.ITag;
import org.openzen.zencode.java.ZenCodeType;
Expand All @@ -15,6 +19,10 @@
import java.util.stream.Collectors;

//TODO breaking: move this to the `.block.` package

/**
* @docParam this <block:minecraft:grass_block>
*/
@ZenRegister
@Document("vanilla/api/block/MCBlock")
@NativeTypeRegistration(value = Block.class, zenCodeName = "crafttweaker.api.blocks.MCBlock")
Expand Down Expand Up @@ -160,6 +168,21 @@ public static float getSlipperiness(Block internal) {
return internal.getSlipperiness();
}

/**
* Sets the slipperiness of this Block.
*
* @param slipperiness The new slipperiness of this Block.
*
* @docParam slipperiness 2
*/
@ZenCodeType.Method
@ZenCodeType.Setter("slipperiness")
public static void setSlipperiness(Block internal, float slipperiness) {

CraftTweakerAPI.apply(new ActionSetBlockProperty<>(internal, "Slipperiness",
slipperiness, internal.slipperiness, value -> internal.slipperiness = value));
}

/**
* Gets the speed factor of this Block.
*
Expand All @@ -172,6 +195,21 @@ public static float getSpeedFactor(Block internal) {
return internal.getSpeedFactor();
}

/**
* Sets the speed factor of this Block.
*
* @param speedFactor The new speed factor of this Block.
*
* @docParam speedFactor 2
*/
@ZenCodeType.Method
@ZenCodeType.Setter("speedFactor")
public static void setSpeedFactor(Block internal, float speedFactor) {

CraftTweakerAPI.apply(new ActionSetBlockProperty<>(internal, "Speed Factor",
speedFactor, internal.speedFactor, value -> internal.speedFactor = value));
}

/**
* Gets the jump factor of this Block.
*
Expand All @@ -184,6 +222,21 @@ public static float getJumpFactor(Block internal) {
return internal.getJumpFactor();
}

/**
* Sets the jump factor of this Block.
*
* @param jumpFactor The new jump factor of this Block.
*
* @docParam jumpFactor 2
*/
@ZenCodeType.Method
@ZenCodeType.Setter("jumpFactor")
public static void setJumpFactor(Block internal, float jumpFactor) {

CraftTweakerAPI.apply(new ActionSetBlockProperty<>(internal, "Jump Factor",
jumpFactor, internal.jumpFactor, value -> internal.jumpFactor = value));
}


/**
* Gets the Item representation of this Block.
Expand Down Expand Up @@ -211,4 +264,87 @@ public static boolean isVariableOpacity(Block internal) {
return internal.isVariableOpacity();
}


/**
* Checks if entities can collide with this Block.
*
* @return True if entities will collide with this Block. False otherwise.
*/
@ZenCodeType.Method
@ZenCodeType.Getter("canCollide")
public static boolean canCollide(Block internal) {

return internal.canCollide;
}

/**
* Sets whether entities can collide with this Block.
*
* @param canCollide Can entities collide with this Block.
*
* @docParam canCollide true
*/
@ZenCodeType.Method
@ZenCodeType.Setter("canCollide")
public static void setCanCollide(Block internal, boolean canCollide) {

CraftTweakerAPI.apply(new ActionSetBlockProperty<>(internal, "Can Collide",
canCollide, internal.canCollide, value -> internal.canCollide = value));
}

/**
* Gets the blast resistance of this Block.
*
* @return The blast resistance of this Block.
*/
@ZenCodeType.Method
@ZenCodeType.Getter("blastResistance")
public static float getBlastResistance(Block internal) {

return internal.blastResistance;
}

/**
* Sets the blast resistance of this Block.
*
* @param resistance The new blast resistance of this Block.
*
* @docParam resistance 2
*/
@ZenCodeType.Method
@ZenCodeType.Setter("canCollide")
public static void setCanCollide(Block internal, float resistance) {

CraftTweakerAPI.apply(new ActionSetBlockProperty<>(internal, "Blast Resistance",
resistance, internal.blastResistance, value -> internal.blastResistance = value));
}

/**
* Gets the material of this Block.
*
* @return The material of this Block.
*/
@ZenCodeType.Method
@ZenCodeType.Getter("material")
public static Material getMaterial(Block internal) {

return internal.material;
}

/**
* Sets the material of this Block.
*
* @param resistance The new material of this Block.
*
* @docParam material 2
*/
@ZenCodeType.Method
@ZenCodeType.Setter("material")
public static void setMaterial(Block internal, Material material) {

CraftTweakerAPI.apply(new ActionSetBlockProperty<>(internal,
"Material", material, internal.material,
value -> internal.material = material, ExpandMaterial::getCommandString));
}

}
12 changes: 10 additions & 2 deletions src/main/resources/META-INF/accesstransformer.cfg
Expand Up @@ -8,13 +8,21 @@ public net.minecraft.tags.Tag func_241287_c_(Ljava/util/Set;)Ljava/lang/Class; #
public-f net.minecraft.tags.Tag field_241282_b_
public-f net.minecraft.tags.Tag field_241283_c_

public-f net.minecraft.block.AbstractBlock field_149764_J #material
public-f net.minecraft.block.AbstractBlock field_235688_at_ #canCollide
public-f net.minecraft.block.AbstractBlock field_235689_au_ #blastResistance
public-f net.minecraft.block.AbstractBlock field_149765_K #slipperiness
public-f net.minecraft.block.AbstractBlock field_226886_f_ #speedFactor
public-f net.minecraft.block.AbstractBlock field_226887_g_ #jumpFactor


public-f net.minecraft.item.Food field_221470_a # value
public-f net.minecraft.item.Food field_221471_b # saturation
public-f net.minecraft.item.Food field_221472_c # meat
public-f net.minecraft.item.Food field_221473_d # canEatWhenFull
public-f net.minecraft.item.Food field_221474_e # fastToEat
public-f net.minecraft.item.Item field_219974_q # food

public-f net.minecraft.item.Item field_219974_q # food
public-f net.minecraft.item.Item field_77777_bU # maxStackSize
public-f net.minecraft.item.Item field_77699_b # maxDamage
public-f net.minecraft.item.Item field_234684_d_ # burnable
Expand Down Expand Up @@ -59,4 +67,4 @@ public net.minecraft.advancements.criterion.PlayerPredicate$IAdvancementPredicat
public net.minecraft.advancements.criterion.StatePropertiesPredicate <init>(Ljava/util/List;)V
public net.minecraft.advancements.criterion.StatePropertiesPredicate$ExactMatcher
public net.minecraft.advancements.criterion.StatePropertiesPredicate$Matcher
public net.minecraft.advancements.criterion.StatePropertiesPredicate$RangedMacher
public net.minecraft.advancements.criterion.StatePropertiesPredicate$RangedMacher

0 comments on commit 1ebd4c3

Please sign in to comment.